Python 如何打印 Pandas DataFrame 的特定行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43772362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:24:04  来源:igfitidea点击:

How to print a specific row of a pandas DataFrame?

pythonpython-3.xpandasindexing

提问by singmotor

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

我有一个庞大的数据框,但出现错误:

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

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.

我已经删除了空值,并检查了 DataFrame 的 dtypes,所以我不知道为什么它在该行失败。

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

如何仅打印出数据框的那一行(索引 159220)?

Thanks

谢谢

回答by piRSquared

When you call locwith 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.

当您loc使用标量值调用时,您会得到一个pd.Series. 该系列将有一个dtype. 如果您想查看数据框中的行,您需要将像 indexer 这样的数组传递给loc.

Wrap your index value with an additional pair of square brackets

用一对额外的方括号括起您的索引值

print(df.loc[[159220]])

回答by kamran kausar

To print a specific row we have couple of pandas method

要打印特定行,我们有几个 Pandas 方法

  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
  1. loc- 它只得到标签,即列名或特征
  2. iloc- 这里 i 代表整数,实际上是行号
  3. ix- 它是标签和整数的混合

How to use for specific row

如何用于特定行

  1. loc
  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
  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]

回答by CK Chen

Use ixoperator:

使用ix运算符:

print df.ix[159220]

回答by Batman

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

听起来你在打电话df.plot()。该错误表明您正在尝试绘制一个没有数字数据的框架。数据类型不应影响您的内容print()

Use print(df.iloc[159220])

print(df.iloc[159220])

回答by vivek SB

print(df.iloc[[index__number]])