【问题标题】:How to create a Dataframe with rows from two other dataframes?如何使用来自其他两个数据框的行创建数据框?
【发布时间】: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)

但我得到的最终数据框没有以正确的方式添加它们

【问题讨论】:

标签: python pandas dataframe


【解决方案1】:

简单的柠檬榨汁

pd.concat([ df1.iloc[300], df2.iloc[2]], axis = 1)

【讨论】:

  • 我应该得到那个?
猜你喜欢
  • 1970-01-01
  • 2019-11-02
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 2020-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多