Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize a dataframe without a header

Tags:

python

pandas

To create a pandas dataframe with a header I can do:

df = pd.DataFrame({'a':[1,2,3], 'b':[4,5,6]})

   a  b
0  1  4
1  2  5
2  3  6

How would I create one without a header, something like:

df = pd.DataFrame([[1,2,3],[4,5,6]])

   0  1  2
0  1  2  3
1  4  5  6
# seems to create three headers, '0', '1', and '2' for the index of the array.
like image 812
David542 Avatar asked Apr 09 '26 18:04

David542


1 Answers

Use zip to interpret the lists as columns instead of lines (if that is your question):

df = pd.DataFrame([*zip([1,2,3],[4,5,6])])
df
>>    0  1
0  1  4
1  2  5
2  3  6
like image 123
Tarifazo Avatar answered Apr 11 '26 08:04

Tarifazo