1

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?

tdy
  • 36,675
  • 19
  • 86
  • 83
Brian Smith
  • 1,200
  • 4
  • 16

1 Answers1

1

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

Emmanuel-Lin
  • 1,848
  • 1
  • 16
  • 31