简体   繁体   English

覆盖 pandas dataframe 列中的值

[英]overwriting a value in a pandas dataframe column

I have 2 dataframe columns that's consist of True, False and NA values, these values are of type str.我有 2 个 dataframe 列,由 True、False 和 NA 值组成,这些值是 str 类型。 How can i merge these dataframe columns together to make another dataframe column that makes the false values overwrite the true values when the 2 dataframe columns are fused together?我如何将这些 dataframe 列合并在一起以制作另一个 dataframe 列,当 2 个 dataframe 列融合在一起时,该列会使错误值覆盖真实值? if the columns were bools i could just use the "&" function but i can't seem to figure it out as the values are strings.如果列是布尔值,我可以只使用“&”function,但我似乎无法弄清楚,因为值是字符串。

i was thinking of adding them together and then trying to do something with that.我想把它们加在一起,然后试着用它做点什么。

df["merged"] = df["column 1"] + df["column 2"] df["合并"] = df["第 1 列"] + df["第 2 列"]

but i think im on the wrong track但我认为我走错了路

You could define a function for this and then apply it across the DataFrame, My first condition means, if the values in the column are the same, retain the value in the third column, assuming that you want the same for 'NA'.您可以为此定义一个 function,然后将其应用于 DataFrame,我的第一个条件意味着,如果列中的值相同,则保留第三列中的值,假设您希望“NA”相同。 The second and third condition, is to choose, either "True" or "False" when the other column has "NA" value.第二个和第三个条件是当另一列具有“NA”值时选择“True”或“False”。 And for all other cases, returns "False", ie if one column has a "True" value and the other "False", this should work:对于所有其他情况,返回“False”,即如果一列具有“True”值而另一列具有“False”,这应该有效:

def and_func(x):
   if x["column 1"] == x["column 2"]:
       return x["column 1"]
   elif ((x["column 1"] == 'NA') & (x["column 2"] != 'NA')):
       return x["column 2"]
   elif ((x["column 2"] == 'NA') & (x["column 1"] != 'NA')):
       return x["column 1"]
   else:
       return "False"


df["merged"] = df.apply(lambda x: and_func(x), axis = 1)

You can convert the True and False string values to booleans and the NA string value to pandas nullable boolean type with:您可以将TrueFalse字符串值转换为布尔值,将NA字符串值转换为 pandas 可空 boolean 类型

d = {'True': True, 'False': False, 'NA': pd.NA}

df['column 1'] = df['column 1'].map(d).astype('boolean')

Then you can perform the logical comparison between the two columns with & .然后,您可以使用&执行两列之间的逻辑比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM