使用条件语句替换 pandas DataFrame 中的条目
Replace an entry in a pandas DataFrame using a conditional statement
我想在给定条件的情况下更改 Dataframe 中条目的值。例如:
d = pandas.read_csv('output.az.txt', names = varname)
d['uld'] = (d.trade - d.plg25)*(d.final - d.price25)
if d['uld'] > 0:
d['uld'] = 1
else:
d['uld'] = 0
我不明白为什么上面的方法不起作用。
感谢您的帮助。
使用 np.where
根据简单的布尔标准设置您的数据:
In [3]:
df = pd.DataFrame({'uld':np.random.randn(10)})
df
Out[3]:
uld
0 0.939662
1 -0.009132
2 -0.209096
3 -0.502926
4 0.587249
5 0.375806
6 -0.140995
7 0.002854
8 -0.875326
9 0.148876
In [4]:
df['uld'] = np.where(df['uld'] > 0, 1, 0)
df
Out[4]:
uld
0 1
1 0
2 0
3 0
4 1
5 1
6 0
7 1
8 0
9 1
至于你做的为什么失败:
In [7]:
if df['uld'] > 0:
df['uld'] = 1
else:
df['uld'] = 0
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-ec7d7aaa1c28> in <module>()
----> 1 if df['uld'] > 0:
2 df['uld'] = 1
3 else:
4 df['uld'] = 0
C:\WinPython-64bit-3.4.3.1\python-3.4.3.amd64\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
696 raise ValueError("The truth value of a {0} is ambiguous. "
697 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 698 .format(self.__class__.__name__))
699
700 __bool__ = __nonzero__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
所以错误是你试图用 True
或 False
计算一个数组,这变得不明确,因为有多个值要比较,因此错误。在这种情况下,你不能真正使用推荐的 any
、all
等,因为你想屏蔽你的 df 并且只设置满足条件的值,[ 有一个解释 pandas 关于这个的网站:http://pandas.pydata.org/pandas-docs/dev/gotchas.html and a related question here: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
np.where
将布尔条件作为第一个参数,如果为真,它将 return 第二个参数,否则,如果为假,则 return 作为第三个参数想要。
更新
再次查看后,您可以使用 astype
:
将布尔系列转换为 int
In [23]:
df['uld'] = (df['uld'] > 0).astype(int)
df
Out[23]:
uld
0 1
1 0
2 0
3 0
4 1
5 1
6 0
7 1
8 0
9 1
我想在给定条件的情况下更改 Dataframe 中条目的值。例如:
d = pandas.read_csv('output.az.txt', names = varname)
d['uld'] = (d.trade - d.plg25)*(d.final - d.price25)
if d['uld'] > 0:
d['uld'] = 1
else:
d['uld'] = 0
我不明白为什么上面的方法不起作用。 感谢您的帮助。
使用 np.where
根据简单的布尔标准设置您的数据:
In [3]:
df = pd.DataFrame({'uld':np.random.randn(10)})
df
Out[3]:
uld
0 0.939662
1 -0.009132
2 -0.209096
3 -0.502926
4 0.587249
5 0.375806
6 -0.140995
7 0.002854
8 -0.875326
9 0.148876
In [4]:
df['uld'] = np.where(df['uld'] > 0, 1, 0)
df
Out[4]:
uld
0 1
1 0
2 0
3 0
4 1
5 1
6 0
7 1
8 0
9 1
至于你做的为什么失败:
In [7]:
if df['uld'] > 0:
df['uld'] = 1
else:
df['uld'] = 0
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-ec7d7aaa1c28> in <module>()
----> 1 if df['uld'] > 0:
2 df['uld'] = 1
3 else:
4 df['uld'] = 0
C:\WinPython-64bit-3.4.3.1\python-3.4.3.amd64\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
696 raise ValueError("The truth value of a {0} is ambiguous. "
697 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 698 .format(self.__class__.__name__))
699
700 __bool__ = __nonzero__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
所以错误是你试图用 True
或 False
计算一个数组,这变得不明确,因为有多个值要比较,因此错误。在这种情况下,你不能真正使用推荐的 any
、all
等,因为你想屏蔽你的 df 并且只设置满足条件的值,[ 有一个解释 pandas 关于这个的网站:http://pandas.pydata.org/pandas-docs/dev/gotchas.html and a related question here: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
np.where
将布尔条件作为第一个参数,如果为真,它将 return 第二个参数,否则,如果为假,则 return 作为第三个参数想要。
更新
再次查看后,您可以使用 astype
:
int
In [23]:
df['uld'] = (df['uld'] > 0).astype(int)
df
Out[23]:
uld
0 1
1 0
2 0
3 0
4 1
5 1
6 0
7 1
8 0
9 1