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

Stop pandas dataframe from converting to vector

I have below pandas dataframe

import pandas as pd
df = pd.DataFrame({'product name': ['laptop', 'printer', 'printer',], 'price': [1200, 150, 1200],  'price1': [1200, 150, 1200]})

Now I want to print one single row

df.iloc[1,:]

This gives below result

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

product name    printer
price               150
price1              150
Name: 1, dtype: object

As can be seen here, above display is a column format. Is it possible to preserve the row format of original dataframe when displaying a single row?

>Solution :

Actually, df.iloc[1,:] is not a pd.DataFrame it is a pd.Series you can check it with type(df.iloc[1, :]). So row or column doesn’t have any sense in these case.

To keep it as a pd.DataFrame you could select a range of rows of length 1: df.iloc[1:2, :] or df.iloc[[1], :]

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