Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas dataframe with N columns

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading