【问题标题】:How to add a new column to multiple pandas dataframes based on a condition?如何根据条件向多个熊猫数据框添加新列?
【发布时间】:2019-08-29 13:08:22
【问题描述】:

我有 17 个数据框,我希望根据某些条件使用 for 循环向其中添加一个新列。 关于如何做到这一点的任何建议?

我有一个名为 custom_region = [] 的列表和 17 个数据帧再次存储在一个名为 CGdfs 的列表中。

CGdfs = [CGdf_2002, CGdf_2003, CGdf_2004, CGdf_2005, CGdf_2006, CGdf_2007, CGdf_2008, CGdf_2009, CGdf_2010, CGdf_2011, CGdf_2012, CGdf_2013, CGdf_2014, CGdf_2015, CGdf_2016, CGdf_2017, CGdf_2018]

我根据循环中的某些条件将新值附加到custom_region。最后,我必须将此列表附加到每个数据框,以便为每个数据框创建一列。

所以,custom_region[0]CGdf_2002
custom_region[1]CGdf_2003 等等..

【问题讨论】:

    标签: python thomson-reuters-eikon refinitiv-eikon-api


    【解决方案1】:

    我不太了解您的问题,但我想您的 custom_region 已经填充了您要附加到 CGdf 数据帧的列。

    如果是这样,您必须遍历 CGdf,并且对于每个数据帧,您将从 custom_region 连接一列

    这个怎么样:

    i = 0
    j = 0 
    
    for i in range(len(CGdfs)) : 
      CGdfs[i] = pd.concat([CGdfs[i],custom_region[j]],axis=1)
      j = j + 1 
    
    
    
    

    【讨论】:

    • 嗨,谢谢。我在 custom_region 中没有任何列。这是一个空列表,我将在循环中添加我的列值。
    猜你喜欢
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 2019-05-02
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 2021-06-25
    相关资源
    最近更新 更多