i have this data frame
import pandas as pd
from deepdiff import DeepDiff
df = pd.DataFrame({'col_new': ['a','b','c,d','d'],
'col_old': ['a','e','d','d'],
'col_val': [True,False,False,True]})
print(df)
col_new col_old col_val
0 a a True
1 b e False
2 c,d d False
3 d d True
i want to replace every False value with the DeepDiff of that row i tried this
df['col_val'] = df['col_val'].where(df['col_val'],DeepDiff(df['col_old'],df['col_new'])['values_changed'])
expected this
col_new col_old col_val
0 a a True
1 b e {'root': {'new_value': 'b', 'old_value': 'e'...
2 c,d d {'root': {'new_value': 'c,d', 'old_value': 'd...
3 d d True
but got this
col_new col_old col_val
0 a a True
1 b e {'root[1]': {'new_value': 'b', 'old_value': 'e...
2 c,d d {'root[1]': {'new_value': 'b', 'old_value': 'e...
3 d d True
Edit: I'd like to thank jezrael for his perfect answer.
But what if there are 10 or 20 columns, should I just type the same code over and over?
from DeepDiff among Rows
No comments:
Post a Comment