[英]replacing values of dataframe column
i have the current dataframe:我有当前的 dataframe:
df = pd.DataFrame({"A":[1,2,-3,-4,5],
"B":[1,-2,3,-4,5]})
i want to replace, just in column A
,我想替换,就在A
列,
all positive values with 1 and all negative values with 0.所有正值都为 1,所有负值都为 0。
i tried to do it this way:我试着这样做:
df[df["A"]>0]["A"] = 1
df[df["A"]<0]["A"] = 0
but that didnt work (dataframe didnt change at all).但这没有用(数据框根本没有改变)。
however the below code did work:但是下面的代码确实有效:
df["A"][df["A"]>0] = 1
df["A"][df["A"]<0] = 0
can anyone tell me what the difference between the two?谁能告诉我两者有什么区别?
why the first one didnt work, while the second one did?为什么第一个不起作用,而第二个起作用?
thanks!谢谢!
To put it simply:简而言之:
df[df["A"]>0]["A"]
gives you a copy of the dataframe df[df["A"]>0]["A"]
为您提供 dataframe 的副本
and df["A"][df["A"]>0]
gives you a view of it.和df["A"][df["A"]>0]
给你一个视图。
The copy isnt linked to the dataframe so changing it wont do anything to the original.副本未链接到 dataframe,因此更改它不会对原始文件做任何事情。
Go to this link for more info: Go 到此链接以获取更多信息:
https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
As an alternative method to above, you can use np.where
and do:作为上述的替代方法,您可以使用np.where
并执行以下操作:
import numpy as np
df['A'] = np.where(df['A'] >0 ,1,0) # 0's will be given 0 here.
which will essentially replace
all +ve's with 1, and everything else with 0.这将基本上用 1 replace
所有 +ve,而用 0 替换其他所有内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.