根据列值

时间:2017-08-15 14:18:28

标签: python pandas

使用:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

a = pd.read_csv('file.csv', na_values=['-9999.0'], decimal=',')
a.index = pd.to_datetime(a[['Year', 'Month', 'Day', 'Hour', 'Minute']])
pd.options.mode.chained_assignment = None

数据框如下:

Index               A    B       C      D
2016-07-20 18:00:00 9   4.0     NaN    2
2016-07-20 19:00:00 9   2.64    0.0    3
2016-07-20 20:00:00 12  2.59    0.0    1
2016-07-20 21:00:00 9   4.0     NaN    2

主要目标是,如果A列上的值为9且D列上的值为2,则将np.nan设置为整行:例如:

输出预期

Index               A    B       C      D
2016-07-20 18:00:00 NaN NaN     NaN    NaN
2016-07-20 19:00:00 9   2.64    0.0     3
2016-07-20 20:00:00 12  2.59    0.0     2
2016-07-20 21:00:00 NaN NaN     NaN    NaN

如果有人可以提供帮助,我将感激不尽。

4 个答案:

答案 0 :(得分:4)

试试这个:

df.loc[df.A.eq(9) & df.D.eq(2)] = [np.nan] * len(df.columns)

演示:

In [158]: df
Out[158]:
                      A     B    C  D
Index
2016-07-20 18:00:00   9  4.00  NaN  2
2016-07-20 19:00:00   9  2.64  0.0  3
2016-07-20 20:00:00  12  2.59  0.0  1
2016-07-20 21:00:00   9  4.00  NaN  2

In [159]: df.loc[df.A.eq(9) & df.D.eq(2)] = [np.nan] * len(df.columns)

In [160]: df
Out[160]:
                        A     B    C    D
Index
2016-07-20 18:00:00   NaN   NaN  NaN  NaN
2016-07-20 19:00:00   9.0  2.64  0.0  3.0
2016-07-20 20:00:00  12.0  2.59  0.0  1.0
2016-07-20 21:00:00   NaN   NaN  NaN  NaN

或者我们可以使用DataFrame.where()方法:

In [174]: df = df.where(~(df.A.eq(9) & df.D.eq(2)))

In [175]: df
Out[175]:
                        A     B    C    D
Index
2016-07-20 18:00:00   NaN   NaN  NaN  NaN
2016-07-20 19:00:00   9.0  2.64  0.0  3.0
2016-07-20 20:00:00  12.0  2.59  0.0  1.0
2016-07-20 21:00:00   NaN   NaN  NaN  NaN

答案 1 :(得分:4)

使用mask,默认情况下创建NaN

df = a.mask((a['A'] == 9) & (a['D'] == 2))
print (df)
                        A     B    C    D
Index                                    
2016-07-20 18:00:00   NaN   NaN  NaN  NaN
2016-07-20 19:00:00   9.0  2.64  0.0  3.0
2016-07-20 20:00:00  12.0  2.59  0.0  1.0
2016-07-20 21:00:00   NaN   NaN  NaN  NaN

boolean indexing并指定NaN

a[(a['A'] == 9) & (a['D'] == 2)] = np.nan
print (a)
                        A     B    C    D
Index                                    
2016-07-20 18:00:00   NaN   NaN  NaN  NaN
2016-07-20 19:00:00   9.0  2.64  0.0  3.0
2016-07-20 20:00:00  12.0  2.59  0.0  1.0
2016-07-20 21:00:00   NaN   NaN  NaN  NaN

<强>计时

np.random.seed(123)
N = 1000000
L = list('abcdefghijklmnopqrst'.upper())

a = pd.DataFrame(np.random.choice([np.nan,2,9], size=(N,20)), columns=L) 
#jez2
In [256]: %timeit a[(a['A'] == 9) & (a['D'] == 2)] = np.nan
10 loops, best of 3: 25.8 ms per loop

#jez2upr
In [257]: %timeit a.loc[(a['A'] == 9) & (a['D'] == 2)] = np.nan
10 loops, best of 3: 27.6 ms per loop

#Wen
In [258]: %timeit a.mul(np.where((a.A==9)&(a.D==2),np.nan,1),0)
10 loops, best of 3: 90.5 ms per loop

#jez1
In [259]: %timeit a.mask((a['A'] == 9) & (a['D'] == 2))
1 loop, best of 3: 316 ms per loop

#maxu2
In [260]: %timeit a.where(~(a.A.eq(9) & a.D.eq(2)))
1 loop, best of 3: 318 ms per loop

#pir1
In [261]: %timeit a.where(a.A.ne(9) | a.D.ne(2))
1 loop, best of 3: 316 ms per loop

#pir2
In [263]: %timeit a[a.A.ne(9) | a.D.ne(2)].reindex(a.index)
1 loop, best of 3: 355 ms per loop

答案 2 :(得分:4)

选项1
这与@ Jezrael的mask解决方案相反。

a.where(a.A.ne(9) | a.D.ne(2))

                        A     B    C    D
Index                                    
2016-07-20 18:00:00   NaN   NaN  NaN  NaN
2016-07-20 19:00:00   9.0  2.64  0.0  3.0
2016-07-20 20:00:00  12.0  2.59  0.0  1.0
2016-07-20 21:00:00   NaN   NaN  NaN  NaN

选项2
pd.DataFrame.reindex

a[a.A.ne(9) | a.D.ne(2)].reindex(a.index)

                        A     B    C    D
Index                                    
2016-07-20 18:00:00   NaN   NaN  NaN  NaN
2016-07-20 19:00:00   9.0  2.64  0.0  3.0
2016-07-20 20:00:00  12.0  2.59  0.0  1.0
2016-07-20 21:00:00   NaN   NaN  NaN  NaN

答案 3 :(得分:2)

或者您可以在.mul

之后尝试使用np.where
a=np.where((df2.A==9)&(df2.D==2),np.nan,1)
df2.mul(a,0)
#one line df.mul(np.where((df.A==9)&(df.D==2),np.nan,1))

                        A     B    C    D
Index                                    
2016-07-20 18:00:00   NaN   NaN  NaN  NaN
2016-07-20 19:00:00   9.0  2.64  0.0  3.0
2016-07-20 20:00:00  12.0  2.59  0.0  1.0
2016-07-20 21:00:00   NaN   NaN  NaN  NaN