【问题标题】:Calculate values shifting by column or looping over column计算按列移动或在列上循环的值
【发布时间】:2018-05-26 18:52:17
【问题描述】:

我有以下数据框,首先为每个队列计算以下数学运算year+n/year.value==2009,然后对每个队列执行平均值

df
             id                                                        
year       2009     2010     2011     2012     2013     2014     2015   
cohort                                                                  
2009.0  72092.0  60513.0  48797.0  40968.0  34919.0  30452.0  26961.0   
2010.0      NaN  73735.0  61899.0  50263.0  42184.0  36150.0  31516.0   
2011.0      NaN      NaN  76809.0  64093.0  51372.0  43277.0  36994.0   
2012.0      NaN      NaN      NaN  69776.0  57621.0  46453.0  39098.0   
2013.0      NaN      NaN      NaN      NaN  71613.0  58996.0  47657.0   
2014.0      NaN      NaN      NaN      NaN      NaN  65430.0  52540.0   
2015.0      NaN      NaN      NaN      NaN      NaN      NaN  67121.0   
2016.0      NaN      NaN      NaN      NaN      NaN      NaN      NaN   
2017.0      NaN      NaN      NaN      NaN      NaN      NaN      NaN  

我将展示我想要执行的数学运算,因为我的英语不好而且数学是一种通用语言:)

对于自 2009 年以来经过 1 年的时间:(n=1)

需要的第一个值 = ((60513.0/72092.0) + (61899.0/73735.0) + (64093.0+76809.0) + (57621.0/69776.0) + (58996.0+71613.0) + (52540.0/65430.0))/6

对于自 2009 年以来经过 2 年的时间:(n=2)

需要的第二个值 = ((48797.0/72092.0) + (50263.0/73735.0) + (51372.0/76809.0) + (46453.0/69776.0) + (47657.0/71613.0))/5

对于自 2009 年以来的任何 3 年过去:(n=3)(最后一个,我认为有了这个,我想要做的循环就会被理解)

需要第三个值 = ((40968.0/72092.0) + (42184.0/73735.0) + (43277.0/76809.0) + (39098.0/69776.0))/4

以此类推,直到最后一个值为

最后一个值 = 26961.0/72092.0

在此先感谢,对不起我的英语

我正在尝试这样的事情,也许它可以帮助

第一个值:

 ((df1.iloc[0,1]/df1.iloc[0,0]) + (df1.iloc[1,2]/df1.iloc[1,1]) + 
 (df1.iloc[2,3]/df1.iloc[2,2]) + (df1.iloc[3,4]/df1.iloc[3,3]) + 
 (df1.iloc[4,5]/df1.iloc[4,4]) + (df1.iloc[5,6]/df1.iloc[5,5]))/6

第二个值:

 ((df1.iloc[0,2]/df1.iloc[0,0]) + (df1.iloc[1,3]/df1.iloc[1,1]) + 
 (df1.iloc[2,4]/df1.iloc[2,2]) + (df1.iloc[3,5]/df1.iloc[3,3]) + 
 (df1.iloc[4,6]/df1.iloc[4,4]))/5

第三个值:

 ((df1.iloc[0,3]/df1.iloc[0,0]) + (df1.iloc[1,4]/df1.iloc[1,1]) + 
 (df1.iloc[2,5]/df1.iloc[2,2]) + (df1.iloc[3,6]/df1.iloc[3,3]))/4

类似这样的东西,但有一个循环

【问题讨论】:

  • 代码是一种通用语言。你试过什么?
  • 我真的是 python 新手,我看了一些问题,但是我尝试过的代码一点用处都没有
  • 我尝试了一些方法,但肯定是更优雅的方式

标签: python python-3.x pandas loops


【解决方案1】:

所以看起来您正在尝试从表中的第一年(列)迭代到最后一年(列)。然后,在你的数学中,除了从你当前迭代到去年的那一年之外,你基本上在做同样的事情。看起来你需要一个循环

numcols = 6 # Set this to the correct value
for year in range(0, numcols-1):
    count = numcols - year
    sum = 0
    for x in range(year, numcols-1):
        sum += df1.iloc[x-year,1+x]/df1.iloc[x-year,x-year]
    print("Answer for this year is: {}".format(sum/count))

【讨论】:

    【解决方案2】:

    这个怎么样?

    ---- 已更新----

    import numpy as np
    
    def sum_with_shift(df, n):
        row_values = []
        for i, row in df.iterrows():
            if (i + n - 1) < df.columns.max():  
                row_values += [row[i] / row[i + n]]
    
        if row_values:
            return np.mean(row_values)
        else:
            return 0
    

    传递你的dfn=1

    sum_with_shift(df, 1)
    
    72092.0 / 60513.0
    73735.0 / 61899.0
    76809.0 / 64093.0
    69776.0 / 57621.0
    71613.0 / 58996.0
    65430.0 / 52540.0
    
    130852.83333333333
    

    传递您的dfn=2

    sum_with_shift(df, 2)
    
    72092.0 / 48797.0
    73735.0 / 50263.0
    76809.0 / 51372.0
    69776.0 / 46453.0
    71613.0 / 47657.0
    
    121713.39999999999
    

    ---- 已更新----

    为了重现性,请尝试运行以下代码来生成您的df

    df_as_json = '{"2009":{"2009":72092.0,"2010":null,"2011":null,"2012":null,"2013":null,"2014":null,"2015":null,"2016":null,"2017":null},"2010":{"2009":60513.0,"2010":73735.0,"2011":null,"2012":null,"2013":null,"2014":null,"2015":null,"2016":null,"2017":null},"2011":{"2009":48797.0,"2010":61899.0,"2011":76809.0,"2012":null,"2013":null,"2014":null,"2015":null,"2016":null,"2017":null},"2012":{"2009":40968.0,"2010":50263.0,"2011":64093.0,"2012":69776.0,"2013":null,"2014":null,"2015":null,"2016":null,"2017":null},"2013":{"2009":34919.0,"2010":42184.0,"2011":51372.0,"2012":57621.0,"2013":71613.0,"2014":null,"2015":null,"2016":null,"2017":null},"2014":{"2009":30452.0,"2010":36150.0,"2011":43277.0,"2012":46453.0,"2013":58996.0,"2014":65430.0,"2015":null,"2016":null,"2017":null},"2015":{"2009":26961.0,"2010":31516.0,"2011":36994.0,"2012":39098.0,"2013":47657.0,"2014":52540.0,"2015":67121.0,"2016":null,"2017":null}}'
    
    df = pd.read_json(df_as_json)
    

    【讨论】:

    • row_values += [row[i] + row[i + n]] 真的会这样吗? row_values += [row[i] / row[i + n]]
    • 抱歉,我尝试抛出一个错误,TypeError: '
    • 你说得对,它应该是“/”而不是“+”。至于错误——我怀疑这是因为数据帧的格式有点不同。尝试用 2015 替换 df.columns.max()
    • 谢谢,但仍然抛出,TypeError: '
    • 我为您添加了一种方法来重现我正在使用的数据框。试试看你是否可以应用这个功能。如果可行,请尝试以相同方式格式化您的数据框,或编辑函数以匹配您的版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-08
    • 2021-10-13
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    相关资源
    最近更新 更多