Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a specific row of a pandas DataFrame?

I have a massive dataframe, and I'm getting the error:

TypeError: ("Empty 'DataFrame': no numeric data to plot", 'occurred at index 159220')

I've already dropped nulls, and checked dtypes for the DataFrame so I have no guess as to why it's failing on that row.

How do I print out just that row (at index 159220) of the data frame?

Thanks

like image 602
singmotor Avatar asked May 04 '17 01:05

singmotor


People also ask

How do I print a specific row in pandas?

You can use the df. loc[[2]] to print a specific row of a pandas dataframe.

How do you print a row in Python?

To print on the same line in Python, add a second argument, end=' ', to the print() function call.

How do I get one row from a DataFrame?

In the Pandas DataFrame we can find the specified row value with the using function iloc(). In this function we pass the row number as parameter.


5 Answers

When you call loc with a scalar value, you get a pd.Series. That series will then have one dtype. If you want to see the row as it is in the dataframe, you'll want to pass an array like indexer to loc.

Wrap your index value with an additional pair of square brackets

print(df.loc[[159220]])
like image 145
piRSquared Avatar answered Oct 14 '22 07:10

piRSquared


To print a specific row we have couple of pandas method

  1. loc - It only get label i.e column name or Features
  2. iloc - Here i stands for integer, actually row number
  3. ix - It is a mix of label as well as integer

How to use for specific row

  1. loc
df.loc[row,column]

For first row and all column

df.loc[0,:]

For first row and some specific column

df.loc[0,'column_name']
  1. iloc

For first row and all column

df.iloc[0,:]

For first row and some specific column i.e first three cols

df.iloc[0,0:3]
like image 37
kamran kausar Avatar answered Oct 14 '22 07:10

kamran kausar


Use ix operator:

print df.ix[159220]
like image 43
CK Chen Avatar answered Oct 14 '22 05:10

CK Chen


Sounds like you're calling df.plot(). That error indicates that you're trying to plot a frame that has no numeric data. The data types shouldn't affect what you print().

Use print(df.iloc[159220])

like image 33
Batman Avatar answered Oct 14 '22 06:10

Batman


If you want to display at row=159220

row=159220

#To display in a table format
display(df.loc[row:row])
display(df.iloc[row:row+1])

#To display in print format
display(df.loc[row])
display(df.iloc[row])
like image 40
ASE Avatar answered Oct 14 '22 06:10

ASE