如何选择行并替换pandas中的一些列

时间:2018-02-20 08:22:04

标签: python pandas

import pandas as pd
dic = {'A': [np.nan, 4, np.nan, 4], 'B': [9, 2, 5, 3], 'C': [0, 0, 5, 3]}
df = pd.DataFrame(dic)
df 

如果我有以下数据

     A  B   C
0   NaN 9   0
1   4.0 2   0
2   NaN 5   5
3   4.0 3   3

我想选择A列为NaN的原始数据,并用np.nan替换B列的值,如下所示。

    A   B   C
0   NaN NaN 0
1   4.0 2.0 0
2   NaN NaN 5
3   4.0 3.0 3

我尝试df[df.A.isna()]["B"]=np.nan,但它不起作用。
根据{{​​3}},我应该按df.iloc选择数据。但问题是如果df有很多行,我就无法通过输入索引选择数据。

3 个答案:

答案 0 :(得分:5)

选项1
你实际上非常接近。在pd.Series.isnull上使用A,并使用B将值分配给df.loc

df.loc[df.A.isnull(), 'B'] = np.nan
df

     A    B  C
0  NaN  NaN  0
1  4.0  2.0  0
2  NaN  NaN  5
3  4.0  3.0  3

选项2
np.where

df['B'] = np.where(df.A.isnull(), np.nan, df.B)
df

     A    B  C
0  NaN  NaN  0
1  4.0  2.0  0
2  NaN  NaN  5
3  4.0  3.0  3

答案 1 :(得分:5)

使用带有倒置条件的maskwhere - 默认情况下替换为NaN s:

df['B'] = df.B.mask(df.A.isnull())
df['B'] = df.B.where(df.A.notnull())

非常类似于使用numpy.where - 定义两个输出:

df['B'] = np.where(df.A.isnull(), np.nan, df.B)
print (df)
     A    B  C
0  NaN  NaN  0
1  4.0  2.0  0
2  NaN  NaN  5
3  4.0  3.0  3

<强>计时

dic = {'A': [np.nan, 4, np.nan, 4], 'B': [9, 2, 5, 3], 'C': [0, 0, 5, 3]}
df = pd.DataFrame(dic)
df = pd.concat([df] * 10000, ignore_index=True)


In [61]: %timeit df['B'] = np.where(df.A.isnull(), np.nan, df.B)
The slowest run took 7.57 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 405 µs per loop

In [62]: %timeit df['B'] = df.B.mask(df.A.isnull())
The slowest run took 70.14 times longer than the fastest. This could mean that an intermediate result is being cached.
1 loop, best of 3: 3.54 ms per loop

In [63]: %timeit df['B'] = df.B.where(df.A.notnull())
The slowest run took 5.65 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 1.04 ms per loop

In [65]: %timeit df.B += df.A * 0
The slowest run took 12.44 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 913 µs per loop

In [67]: %timeit df.loc[df.A.isnull(), 'B'] = np.nan
The slowest run took 4.56 times longer than the fastest. This could mean that an intermediate result is being cached.
100 loops, best of 3: 2.88 ms per loop

答案 2 :(得分:5)

因为我的同行采取了合理的选择......

df.B += df.A * 0
df

     A    B  C
0  NaN  NaN  0
1  4.0  2.0  0
2  NaN  NaN  5
3  4.0  3.0  3