I wasn't sure where the date, 1998-12-30 00:00
, came from since it wasn't in your first link so I've appended it to the column of dates before inserting it into the transposed DataFrame
.
(我不确定日期1998-12-30 00:00
来自何处,因为它不在您的第一个链接中,因此在将其插入转置后的DataFrame
之前,我将其附加到了日期列中。)
I also wasn't sure what you wanted to name the remaining columns. (我也不确定您要命名其余的列。)
You wanted to keep the Date
column as it is while transposing the others, which may be done like so: (您希望在转置其他Date
保持“ Date
列不变,可以这样进行:)
import pandas as pd
data = pd.DataFrame.from_dict({
"Date": ["", "", "", "1998-12-02 00:00", "1998-12-09 00:00", "1998-12-16 00:00", "1998-12-23 00:00"],
"Unnamed: 1": ["KN.A24", "KN.M6", "index, 1976 = 100", "142", "126", "126", "144"],
"Unnamed: 2": ["KN.A24", "KN.M9", "%, avg index", "110.35", "254", "412.36", "134.5"],
"Unnamed: 3": ["KN.A24", "KN.M10", "%, avg index", "211.5", "185.25", "172", "177.5"],
"Unnamed: 7": ["KN.A24", "KN.M12", "%, change", "38.73517787", "60.51502146", "75.11312217", "82.77511962"],
"Unnamed: 8": ["KN.A24", "KN.M11", "%, change", "89.9864682", "101.7985612", "112.2486289", "115.2112676"],
})
df = data.iloc[:, 1:].T.rename(columns={0: "Indicator", 1: "Measure", 2: "Unit", 3: "Value"}).reset_index(drop=True)
df.insert(0, "Date", data["Date"].append(pd.Series(["1998-12-30 00:00"])).iloc[3:].reset_index(drop=True))
print(df.to_string(index=False))
# Date Indicator Measure Unit Value 4 5 6
# 1998-12-02 00:00 KN.A24 KN.M6 index, 1976 = 100 142 126 126 144
# 1998-12-09 00:00 KN.A24 KN.M9 %, avg index 110.35 254 412.36 134.5
# 1998-12-16 00:00 KN.A24 KN.M10 %, avg index 211.5 185.25 172 177.5
# 1998-12-23 00:00 KN.A24 KN.M12 %, change 38.73517787 60.51502146 75.11312217 82.77511962
# 1998-12-30 00:00 KN.A24 KN.M11 %, change 89.9864682 101.7985612 112.2486289 115.2112676