I have pandas table where I want to create new column and fill data based on another columns values. I also want to know, if new columns value is updated So I have dictionary like this:
update_values = {"Groub_A": {"aadff2": "Mark", "aasd12": "Otto", "asdd2": "Jhon"},"Groub_B": {"aadfaa": "Josh", "aa1113": "Math", "967323sd": "Marek"}}
And I want to my table look like this:
Column_1 | Column_new_2 | Column_new_3
aadff2 | Mark | Groub_A
aadff2 | Mark | Groub_A
aasd12 | Otto | Groub_A
asdd2 | Jhon | Groub_A
967323sd | Marek | Groub_B
967323sd | Marek | Groub_B
aa1113 | Math | Groub_B
So far I have just copied Column_1 and use df.replace("Column_new_2":update_values["Groub_A"])
and same thing with groub_B, but then don't know how to make Column_new_3?
There must be a easy solution, but I just can't figure it out.
Consider a nested list/dict comprehension to build a list of dictionaries to pass into pandas.DataFrame.from_records
. Then, left merge
against current data frame for New columns.
new_data = [
{"Column_1": k, "Column_new_2": v, "Column_new_3": gk}
for gk, gv in update_values.items()
for k, v in gv.items()
]
current_df = current_df.merge(
pd.DataFrame.from_records(new_data), on = "Column_1", how = "left"
)