Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

python-3.x - 无法使用熊猫获得正确的数据转发(Unable to obtain proper Transponse of data using pandas)

This is my Raw Data screen shot

(这是我的原始数据屏幕截图)

I want to Transpose my Data as shown here

(我想转置我的数据,如下所示)

I tried to transpose using :

(我试图使用转置:)

DataT = pd.melt(Data, id_vars=['Date'],var_name='indicator', value_name='Value')

I want to transpose the data using pandas ....columns of data are : Date , Unnamed: 1 , Unnamed: 2, Unnamed: 3, Unnamed: , Unnamed: 8.

(我想使用熊猫转置数据....数据列为:日期,未命名:1,未命名:2,未命名:3,未命名:,未命名:8。)

  ask by Pavan Suvarna translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...