-1

I have a dataframe that looks like:

enter image description here

I would like to have all the rain and temperature data in 2 seperate columns

enter image description here

  • Please provide a [mcve], as well as the entire error output. Also, please do not share information as images unless absolutely necessary. See: https://meta.stackoverflow.com/q/303812, https://meta.stackoverflow.com/q/285551, https://idownvotedbecau.se/imageofcode, https://idownvotedbecau.se/imageofanexception/. – AMC Nov 28 '20 at 02:39
  • You can use print(df.iloc[0:100].to_dict) to print our sample data. – frankr6591 Nov 28 '20 at 02:46
  • Read Q/A 10 in the dup link. – Quang Hoang Nov 28 '20 at 03:45

2 Answers2

0

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:

enter image description here

Julio Reckin
  • 146
  • 1
  • 10
  • Thank you that works good for the values however, the column names are still not showing. Index(['0', '0', '0'], dtype='object') – Niki Bowie Nov 28 '20 at 04:00
  • Okay then you just have to change the name 0 in the code above of all to: "0" then it should work – Julio Reckin Nov 28 '20 at 14:45
  • No problem can you give me an upvote on my post, when you are satisfied with my answer? This would allow me to finally comment on other posts – Julio Reckin Nov 28 '20 at 15:26
0

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
frankr6591
  • 1,211
  • 1
  • 8
  • 14