【发布时间】:2019-04-20 10:48:58
【问题描述】:
我正在尝试将数据帧拼接在一起。我有一个包含两个数据帧之间匹配数据的行的列表。
因此,假设数据框 1 中的第 300 行与数据框 2 中的第 2 行指的是同一家公司。目前我正在使用两个原始数据框创建一个新数据框,但它没有以正确的方式添加它们。
df_final = pd.DataFrame( df1.iloc[300], df2.iloc[2]])
这样做,我得到两行数据,其中包含来自两个数据帧的数据,我真正想要的是一行,两个数据帧的数据水平添加。因此,如果每个数据框有 5 列,我想要有 10 列的东西。
import pandas as pd
position_list = [(0, 85, 83),
(1, 134, 67),
(2, 78, 50),
(3, 89, 83),
(4, 90, 83),
(5, 91, 83)]
ammended_results= Name of Applicant CMU ID CMU Name Capacity Awarded Classification Capacity (MW) Duration (Years) Clearing price (£) Yearly CMU Funding (£) Contract Length Funding (£) New Generation Existing Generation New Capacity Existing Capacity
1 SSE Generation Ltd. BURGH1 Burghfield Yes Existing Generating CMU 44.034 1.0 19.4 854259.6 854259.6 0 1 0.000 44.034
2 SSE Generation Ltd. CBEU01 Deanie 1 Yes Existing Generating CMU 15.886 1.0 19.4 308188.4 308188.4 0 1 0.000 15.886
3 SSE Generation Ltd. CBEU02 Deanie 2 Yes Existing Generating CMU 15.886 1.0 19.4 308188.4 308188.4 0 1 0.000 15.886
...
ammended_register = Unique CMU Identifier Type Delivery Year Name of Applicant CM Unit Name
...
BURG18 T-4 2018 SSEPG (Operations) Limited Burghfield
...
基本上我想从结果数据帧的第一个条目与寄存器数据帧中的第 85 个条目。并使用所有这些信息创建一个新的数据框。
目前我正在使用这个
df_list=[]
for i in range(len(position_list)):
result = position_list[i][1]
df_final = pd.concat(ammended_results.iloc[i], ammended_register.iloc[results]])
df.reset_index(inplace=True, drop=True)
df_list.append(df)
fr = pd.concat(df_list, axis=0)
但我得到的最终数据框没有以正确的方式添加它们
【问题讨论】:
-
请发minimal reproducible example。我认为你只是想要
pd.concat,但我不确定。 -
我已经添加了这些