Existing data looks like the following:
Year Month Data
0 2021 11 45
1 2021 12 77
2 2022 1 234
3 2022 2 12
4 2022 3 432
5 2022 4 888
I want to rearrange this table to look like this:
Year 1 2 3 4 5 6 7 8 9 10 11 12
0 2021 45 77
1 2022 234 12 432 888
2 2023
I have found a way to accomplish this by using nested loops but hoping there is a better way. I have quite a few tables to work with and using my method is going to be awkward at best. More importantly, it requires a blank dataframe for the data to be inserted into. This is a static framework and would require regular updates depending on the format of the original dataframe.
Here is the code:
import pandas as pd
# had to create this blank dataframe to put the data into
data = {'Year': ['2021', '2022', '2023'],
'1': ['', '', ''],
'2': ['', '', ''],
'3': ['', '', ''],
'4': ['', '', ''],
'5': ['', '', ''],
'6': ['', '', ''],
'7': ['', '', ''],
'8': ['', '', ''],
'9': ['', '', ''],
'10': ['', '', ''],
'11': ['', '', ''],
'12': ['', '', '']
}
new_table = pd.DataFrame(data)
# created this dataframe to represent an example of how the original dataframes are received
data2 = {'Year': ['2021', '2021', '2022', '2022', '2022', '2022'],
'Month': ['11', '12','1','2','3','4'],
'Data': ['45','77','234','12','432','888']}
orig_table = pd.DataFrame(data2)
for k in range(len(new_table)):
for i in range(12):
for x in range(len(orig_table)):
if orig_table.iloc[x,0] == new_table.iloc[k,0] and orig_table.iloc[x,1] == new_table.columns[i+1]:
new_table.iloc[k,i+1] = orig_table.iloc[x,2]
print('\n',orig_table)
print('\n Updated Test df\n',new_table)
Any help would be greatly appreciated!