在 Pandas 数据框中查找包含 inf 的单元格的行位置和列名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42790542/
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-09-14 03:11:30  来源:igfitidea点击:

find row positions and column names of cells contanining inf in pandas dataframe

pythonpandasdataframe

提问by gabboshow

How can I retrieve the column names and the rows of all the cells that contain inf in a multicolumns panda datarame df?

如何在多列Pandas数据帧中检索包含 inf 的所有单元格的列名和行df

I have tried

我试过了

inds = np.where(np.isinf(df)==True)

but I dont have the expected result

但我没有预期的结果

回答by MaxU

row positions:

行位置:

df.index[np.isinf(df).any(1)]

column names:

列名:

df.columns.to_series()[np.isinf(df).any()]

Demo:

演示:

In [163]: df
Out[163]:
minor             AAPL                        GS
             Adj Close        Volume   Adj Close     Volume
Date
2017-03-01  139.789993  3.627240e+07  252.710007  5218300.0
2017-03-02  138.960007           inf         inf  3020000.0
2017-03-03  139.779999  2.110810e+07  252.889999  3163700.0
2017-03-06  139.339996           inf         inf  2462300.0
2017-03-07  139.520004  1.726750e+07  250.899994  2414900.0
2017-03-08  139.000000           inf         inf  3574400.0
2017-03-09  138.679993  2.206520e+07  250.179993  3055700.0
2017-03-10  139.139999  1.948800e+07  248.380005  3357800.0
2017-03-13  139.199997  1.704240e+07  248.160004  1782700.0

In [164]: df.index[np.isinf(df).any(1)]
Out[164]: DatetimeIndex(['2017-03-02', '2017-03-06', '2017-03-08'], dtype='datetime64[ns]', name='Date', freq=None)

In [165]: df.columns.to_series()[np.isinf(df).any()]
Out[165]:
minor
AAPL   Volume        (AAPL, Volume)
GS     Adj Close    (GS, Adj Close)
dtype: object