Pandas dataframe with N columns

Question:

I need to use Python with Pandas to write a DataFrame with N columns. This is a simplified version of what I have:

Ind=[[1, 2, 3],[4, 5, 6],[7, 8, 9],[10, 11, 12]]

DAT = pd.DataFrame([Ind[0],Ind[1],Ind[2],Ind[3]], index=None).T
DAT.head()

Out

    0   1   2   3
0   1   4   7   10
1   2   5   8   11
2   3   6   9   12

This is the result that I want, but my real Ind has 121 sets of points and I really don’t want to write each one in the DataFrame’s argument. Is there a way to write this easily? I tried using a for loop, but that didn’t work out.

Asked By: felipe

||

Answers:

You can just pass the list directly:

data = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
df = pd.DataFrame(data, index=None).T
df.head()

Outputs:

    0   1   2
0   1   2   3
1   4   5   6
2   7   8   9
3  10  11  12
Answered By: mtoor
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.