I am passing in a single dataframe for performing various other data cleansing processes. While doing so, one of the process I am unable to complete without having another dataframe.
data= {'ID':[1,2], '2020-11-01' :[10,15], '2020-11-02':[43,35]}
df1 = pd.DataFrame.from_dict(data)
df1.head()
ID 2020-11-01 2020-11-02
0 1 10 43
1 2 15 35
I would need to convert those dates as rows so used melt
df2 = df1.melt(id_vars = ["ID"], var_name = "ReportDate", value_name= "Units")
df2.head()
ID ReportDate Units
0 1 2020-11-01 10
1 2 2020-11-01 15
2 1 2020-11-02 43
3 2 2020-11-02 35
Now I need to drop everything from df1 and need to capture the df2 details to df1.
I tried to drop all columns from df1(using inplace=True) and then do
df1["ID"] = df2["ID"]
df1["ReportDate"] = df2["ReportDate"]
df1["Units] = df2[Units]
df1.head()
ID ReportDate Units
0 1 2020-11-01 10
1 2 2020-11-01 15
But I ended up with only 2 rows since the previous shape of df1 was 2x3
I need my output to look like
df1.head()
ID ReportDate Units
0 1 2020-11-01 10
1 2 2020-11-01 15
2 1 2020-11-02 43
3 2 2020-11-02 35
How do I get df1 to have all the contents of df2?
f1.head()as you show the earlier stages.df1 = df2not satisfy your requirements? orimport copy df1 = copy.deepcopy(df2)