Home > OS >  Arranging a Dataframe to a specific format using a loop in Pandas Python
Arranging a Dataframe to a specific format using a loop in Pandas Python

Time:10-06

I'm a python noob. I have the following data frame which needs to be arranged as the example output below. Please note this is a simplified version of the Data frame and the data continues for 3 months. Source file is an unstructured txt file.

Dataframe

0 36 43 79
1 TDY 1.2 TOM 1.2
2 SPT 1.4 1WK 1.4
3 2WK 1.5 1MO 1.6
4 2MO 1.7 3MO 1.7
5 6MO 1.4 9MO 1.8
6 1YR 1.7 2YR 1.3
7 3YR 1.2 4YR 1.2
8 5YR 1.4 6YR 1.4
9 TDY 1.2 TOM 1.2
10 SPT 1.4 1WK 1.4
11 2WK 1.5 1MO 1.6
12 2MO 1.7 3MO 1.7
13 6MO 1.4 9MO 1.8
14 1YR 1.7 2YR 1.3
15 3YR 1.2 4YR 1.2
16 5YR 1.4 6YR 1.4
17 TDY 1.2 TOM 1.2
18 SPT 1.4 1WK 1.4
19 2WK 1.5 1MO 1.6
20 2MO 1.7 3MO 1.7
21 6MO 1.4 9MO 1.8
22 1YR 1.7 2YR 1.3
23 3YR 1.2 4YR 1.2
24 5YR 1.4 6YR 1.4

Output required

TDY TOM SPT 1WK 2WK 1MO 2MO 3MO 6MO 9MO 1YR 2YR 3YR 4YR 5YR 6YR
1.2 1.2 1.4 1.4 1.5 1.6 1.7 1.7 1.4 1.8 1.7 1.3 1.2 1.2 1.4 1.4
1.2 1.2 1.4 1.4 1.5 1.6 1.7 1.7 1.4 1.8 1.7 1.3 1.2 1.2 1.4 1.4
1.2 1.2 1.4 1.4 1.5 1.6 1.7 1.7 1.4 1.8 1.7 1.3 1.2 1.2 1.4 1.4

image

CodePudding user response:

You can use pd.melt

ordered_columns = ["TDY", "TOM", "SPT", "1WK", "2WK", "1MO", "2MO", "3MO",
                   "6MO", "9MO", "1YR", "2YR", "3YR", "4YR", "5YR", "6YR"]

df = pd.concat([df[["0", "43"]].melt(value_name="headers"), df[["36", "79"]].melt(value_name="values")], axis=1)[["headers", "values"]]
grouped_map = df.groupby("headers")["values"].apply(list).to_dict()
df_final = pd.DataFrame(grouped_map).reindex(columns=ordered_columns)
print(df_final)

   TDY  TOM  SPT  1WK  2WK  1MO  2MO  3MO  6MO  9MO  1YR  2YR  3YR  4YR  5YR  6YR
0  1.2  1.2  1.4  1.4  1.5  1.6  1.7  1.7  1.4  1.8  1.7  1.3  1.2  1.2  1.4  1.4
1  1.2  1.2  1.4  1.4  1.5  1.6  1.7  1.7  1.4  1.8  1.7  1.3  1.2  1.2  1.4  1.4
2  1.2  1.2  1.4  1.4  1.5  1.6  1.7  1.7  1.4  1.8  1.7  1.3  1.2  1.2  1.4  1.4
  • Related