简体   繁体   中英

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. 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? 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.

i was thinking of adding them together and then trying to do something with that.

df["merged"] = df["column 1"] + df["column 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'. The second and third condition, is to choose, either "True" or "False" when the other column has "NA" value. And for all other cases, returns "False", ie if one column has a "True" value and the other "False", this should work:

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:

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 & .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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