2

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.
3
  • 1
    Mmm, how do you propose to make use of this dataframe with no column names? Why not just have a 2D numpy array, which will be much faster? Commented Dec 17, 2018 at 19:20
  • I was thinking I could use the index, perhaps it's already doing that above just explicitly? Commented Dec 17, 2018 at 19:21
  • You just get an autoincrementing column name. Use an array and array indexing. Unless there's a specific function in pandas that you need, I think this will be both inefficient and confusing Commented Dec 17, 2018 at 19:22

2 Answers 2

7

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
Sign up to request clarification or add additional context in comments.

1 Comment

yes, sorry my question wasn't clear, this is the answer that I was looking for -- thanks.
2

Well what you could do, having created the dataframe, is:

df.columns = ['' for i in range(df.shape[1])]

However, this is not advisable as the columns are necessary if you want to slice the dataframe

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.