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 : Convert 1D dataframe to 2D dataframe

I have pandas dataframe whose shape is (4628,).
How do I change the shape of dataframe to (4628,1)?

>Solution :

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

You might have a Series, you can turn it into DataFrame with Series.to_frame

s = pd.Series(range(10))

out = s.to_frame('col_name') # or .to_frame()
print(s.shape)

(10,)

print(out.shape)

(10, 1)

Don’t know how you get that Series, if you use a label like df['a'], you can try passing a list of labels instead, like df[['a']]

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