Stop pandas dataframe from converting to vector

Viewed 29

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

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?

1 Answers

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], :]

Related