简体   繁体   English

如何根据另一列对一行中的特定列求和?

[英]How can I sum specific columns in a row, depending on another column?

could you please help my with the following issue: 您能帮我解决以下问题吗?

Sample File : 样本文件:

在此处输入图片说明

I try to aggregate the sum of 6 months, up from a specific start date per row. 我尝试汇总每个行的特定开始日期开始的6个月的总和。

The sum should be shown in a new column (Sum 6 months from startdate) 总金额应显示在新列中(自开始日期起6个月的总和)

My first thought would be to get it with the following code: 我首先想到的是使用以下代码来获取它:

df['sum_6_months'] = df.loc[:,'01.2018':'06.2018'].apply(sum, axis=1)

but this code is not individually and only for the timeframe (01.18-06.18) in all rows. 但是此代码不是单独的,并且仅适用于所有行的时间范围(01.18-06.18)。

df = pd.DataFrame(np.array([[1, 5, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4,1], [1, 5, 3, 4, 5, 6, 7, 7,8,2,5,7,3,4,2],[1,5,3, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4],
                             [1, 5, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4,3], [1, 5, 3, 4, 5, 6, 7, 7,8,2,5,7,3,4,4],[1,5,5, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4],
                             [1, 5, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4,5], [1, 5, 3, 4, 5, 6, 7, 7,8,2,5,7,3,4,5],[1,5,2, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4],
                             [1, 5, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4,6], [1, 5, 3, 4, 5, 6, 7, 7,8,2,5,7,3,4,2],[1,5,5, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4],
                             [1, 5, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4,4], [1, 5, 3, 4, 5, 6, 7, 7,8,2,5,7,3,4,2],[1,5,1, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4]]),
                   columns=['01.2018', '02.2018', '03.2018', '04.2018', '05.2018','06.2018', '07.2018', '08.2018',
                            '09.2018','10.2018', '11.2018', '12.2018','01.2019', '02.2019', '03.2019'])

date = [01.2018, 03.2018,04.2018,05.2018,03.2018,01.2018, 03.2018,04.2018,05.2018,03.2018,01.2018, 03.2018,04.2018,05.2018,03.2018]
df['Startdate']= date

First, calculate the number of columns to skip in each row: 首先,计算每行要跳过的列数:

df2['StartCol'] = 1 + df2.columns[1:].searchsorted(df2.Startdate)

The 1: skips the Startdate column. 1:跳过“开始Startdate列。 Then "roll" that many columns to the left, so they wrap around and end up at the end of each row, take the first 6, and sum: 然后将那么多列“滚动”到左侧,以便它们环绕并最终在每一行的末尾,取前6个,然后求和:

np.roll(df2.iloc[:, 1:], -df2.StartCol)[:,:6].sum(1)

That gives you: 那给你:

[27, 28, 27, 21, 26, 27, 29, 27, 21, 25, 27, 25, 25, 18, 23]

Which you can store in a new column if you like. 如果愿意,可以将其存储在新列中。

df['Startdate']=df['Startdate'].astype(str).str.rjust(7,'0')

df_columns = df.columns.tolist()
def get_sum_six(df_list):
    start_date_index = df_columns.index(df_list[-1])
    df_list = df_list[0:-1]
    sum_of_six = sum(df_list[start_date_index: start_date_index + min(len(df_list)-start_date_index, 6)])
    return (sum_of_six)

df['sum_last_six'] = df.apply(lambda x: get_sum_six(x.tolist()), axis=1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas dataframe,如何按多列分组并为特定列应用总和并添加新的计数列? - Pandas dataframe, how can I group by multiple columns and apply sum for specific column and add new count column? 如何对与pandas DataFrame中另一列的特定值对应的列值求和? - How can I sum column values that corrispond to a specific value of another column in a pandas DataFrame? 如何编写 Python 代码来查找特定行值的 Pandas DF 中列的值的总和? - How can I write the Python code to find the sum of values of a column in a Pandas DF for a specific row value? 如何根据另一列的值拆分数字列? - How can I split a numerical column depending on the value of another column? 如果另一列具有特定值,我如何在 python 中分组并创建包含列信息的列? - How can I group by in python and create columns with information of a column if another column has a specific value? 根据另一列(Pandas)中的条件替换每行中的列中的内容 - Replacing contents in Columns in each row depending on condition in another column(Pandas) 我有一个包含几列的数据框,并且想要具有+1行的特定列的行总和 - I have a data frame with Few columns and want sum of rows of a specific column with +1 row 熊猫如何根据其他列中的值汇总一列的总和 - pandas how to aggregate sum on a column depending on values in other columns 如何对具有相同列和行的值求和 - How can I sum values with same column and row 如何对特定索引给出的列求和? - How can I sum a column given by a specific index?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM