python - pandas .ix 中是否可以有两个条件?

标签 python pandas dataframe

我想知道是否有一种方法可以使 .ix 具有多个条件。更具体地说,我想做的是改变这一点:

In [66]: df_test
Out[66]: 
      A          B         C         D         E
0 -0.013863      False -0.546036  0.373015  1.002579
1  1.275009          2  0.447672 -0.393775 -1.509525
2 -0.517209          0  0.543322  2.434393  0.348002
3  1.768921          0 -1.015705  1.121779  1.548792
4  0.575418        NaN -1.803939  0.099772  0.508620
5  0.722897   0.519641  0.435199 -0.059685 -0.585716

地点:

In [67]: type(df_test.iloc[0,1])
Out[67]: bool

In [68]: type(df_test.iloc[1,1])
Out[68]: str

In [69]: type(df_test.iloc[2,1])
Out[69]: str

In [70]: type(df_test.iloc[3,1])
Out[70]: int

对此:

      A          B         C         D         E
0 -0.013863        NaN -0.546036  0.373015  1.002579
1  1.275009          2  0.447672 -0.393775 -1.509525
2 -0.517209          0  0.543322  2.434393  0.348002
3  1.768921          0 -1.015705  1.121779  1.548792
4  0.575418        NaN -1.803939  0.099772  0.508620
5  0.722897   0.519641  0.435199 -0.059685 -0.585716

似乎位置 [0,1][3,1] 中的项目都是 == False,因此,当我尝试 df_test.ix[df_test.B == False, 'B'] = np.nan 时,这两项都变成了 NaN

当我尝试 df_test.ix[df_test.B == False 且 type(df_test.B) == bool, 'B'] = np.nan 时,出现以下错误:KeyError:“无法使用单个 bool 值来索引 setitem”

任何想法将不胜感激。

编辑

In [133]: df_test
Out[133]: 
      A         B         C         D         E
0 -0.013863     False         1  0.373015  1.002579
1  1.275009         2  0.447672 -0.393775 -1.509525
2 -0.517209         0         3  2.434393  0.348002
3  1.768921         0       NaN  1.121779  1.548792
4  0.575418       NaN  -1.80394  0.099772  0.508620
5  0.722897  0.519641  0.435199 -0.059685 -0.585716
...

In [134]: df_test.dtypes
Out[134]: 
A    float64
B     object
C     object
D    float64
E    float64
dtype: object

In [139]: type(df_test['B'][0])
Out[139]: bool

In [140]: type(df_test['B'][1])
Out[140]: str

In [141]: type(df_test['B'][2])
Out[141]: str

In [142]: type(df_test['B'][3])
Out[142]: int

In [143]: type(df_test['B'][4])
Out[143]: float

In [144]: df_test['B'] == False
Out[144]: 
0     True
1    False
2    False
3     True
4    False
5    False
6    False
7    False
8    False
9    False
Name: B, dtype: bool

编辑2 请参阅下面的复制方式

In [226]: df_test = pd.DataFrame(np.random.randn(5,5), columns=list('ABCDE'))

In [227]: df_test
Out[227]: 
      A         B         C         D         E
0  0.609775  0.205708 -0.015081  2.096414  0.121245
1  1.223234 -1.299398  1.238649  0.216846 -0.789828
2  0.446873  1.734404 -0.675568 -1.203400  0.053905
3  0.286693 -0.080294 -0.115739 -0.195039  0.400201
4  0.519230  1.939370 -0.424466  0.102137 -0.724420

In [228]: df_test.iloc[0,1] = False

In [229]: df_test.iloc[1,1] = '0'

In [230]: df_test.iloc[2,1] = 0

In [231]: df_test.iloc[3,1] = '2'

In [232]: df_test.B == False
Out[232]: 
0     True
1    False
2     True
3    False
4    False
Name: B, dtype: bool

最佳答案

这行代码可以实现您想要的功能。它将 False 值映射到 np.nan:

df_test['B'] = df_test['B'].map(lambda x:np.nan if x == False else x)

编辑:

更好的方法就是

df[df == False] = np.nan

实际解决方案

经过一番努力,似乎 map() 可以满足多种条件,并且下面的效果很好:

df_test['B'] = df_test['B'].map(lambda x: np.nan if ((type(x) == bool) & (x == False)) else x)

关于python - pandas .ix 中是否可以有两个条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36603671/

相关文章:

python - 如何根据某些条件(包括日期时间)映射多个 Pandas Dataframe 中的值?

python - 在 Python 中将不需要的日期时间格式的数据值更改为带破折号的数字

python - 使用条件从索引 1 开始替换数据框中的所有列

python - 列出多个匹配实例的交集

python - flask-wtform 占位符行为

python - 如何使用 Pandas 中的多列映射函数?

python - 使用 Excel Pandas 中的浮点值填充字典时出现问题

python - 遍历 pandas 中的前 N ​​行

python - 使用 PIL 从图像中获取像素

python - 我如何使用 python 2.6 安装 zope 接口(interface)?