I have a dataframe that looks like:
I would like to have all the rain and temperature data in 2 seperate columns
I have a dataframe that looks like:
I would like to have all the rain and temperature data in 2 seperate columns
You can solve it like this:
df_months = df.iloc[::3, :].reset_index(drop=True).rename(columns={0:"Month"})
df_rain = df.iloc[1::3, :].reset_index(drop=True).rename(columns={0:"Rain"})
df_temp = df.iloc[2::3, :].reset_index(drop=True).rename(columns={0:"Temperature"})
df_all = pd.concat([df_months,df_rain,df_temp],axis=1)
Output:
Transform based on column1 collection:
df2 = pd.DataFrame({k:list(df[df.column1 ==k].v2) for k in df.column1.unique()})
print(df)
print("=======")
print(df2)
output
v1 v2
0 Month 1
1 Rain 24
2 Temperature 69
3 Month 2
4 Rain 8
5 Temperature 42
6 Month 3
7 Rain 97
8 Temperature 17
==========
Month Rain Temperature
0 1 24 69
1 2 8 42
2 3 97 17