在pandas数据帧中查找连接inf的单元格的行位置和列名称

时间:2017-03-14 15:47:19

标签: python pandas dataframe

如何在多列熊猫数据框df中检索包含inf的所有单元格的列名和行?

我试过了

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

但我没有预期的结果

1 个答案:

答案 0 :(得分:9)

行位置:

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

列名:

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

演示:

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