简体   繁体   English

将每行不同列数的值相加

[英]Sum up value in different numbers of columns for each row

I have a data frame including number of sold tickets in different price buckets for each flight.我有一个数据框,包括每个航班在不同价格范围内售出的机票数量。

For each record/row, I want to use the value in one column as an index in iloc function, to sum up values in a specific number of columns.对于每条记录/行,我想将一列中的值用作 iloc function 中的索引,以汇总特定列数中的值。

Like, for each row, I want to sum up values from column index 5 to value in ['iloc_index']就像,对于每一行,我想将列索引 5 中的值总结为 ['iloc_index'] 中的值

I tried df.iloc[:, 5:df['iloc_index']].sum(axis=1) but it did not work.我试过df.iloc[:, 5:df['iloc_index']].sum(axis=1)但它没有用。

sample data:样本数据:

   A  B  C  D  iloc_value  total
0  1  2  3  2     1
1  1  3  4  2     2
2  4  6  3  2     1

for each row, I want to sum up the number of columns based on the value in ['iloc_value']对于每一行,我想根据 ['iloc_value'] 中的值总结列数

for example,例如,

  • for row0, I want the total to be 1+2对于第 0 行,我希望总数为 1+2
  • for row1, I want the total to be 1+3+4对于第 1 行,我希望总数为 1+3+4
  • for row2, I want the total to be 4+6对于第 2 行,我希望总数为 4+6

EDIT: I quickly got the results this way:编辑:我很快就这样得到了结果:

First define a function that can do it for one row:首先定义一个 function 可以做一行:

def sum_till_iloc_value(row):
    return sum(row[:row['iloc_value']+1])
    

Then apply it to all rows to generate your output:然后将其应用于所有行以生成您的 output:

df_flights['sum'] = df_flights.apply(sum_till_iloc_value, axis=1)
   A  B  C  D  iloc_value  sum
0  1  2  3  2           1    3
1  1  3  4  2           2    8
2  4  6  3  2           1   10

PREVIOUSLY: Assuming you have information that looks like: 以前: 假设您有如下信息:

 df_flights = pd.DataFrame({'flight':['f1', 'f2', 'f3'], 'business':[2,3,4], 'economy':[6,7,8]}) df_flights
 flight business economy 0 f1 2 6 1 f2 3 7 2 f3 4 8

you can sum the columns you want as below:你可以总结你想要的列,如下所示:

 df_flights['seat_count'] = df_flights['business'] + df_flights['economy']

This will create a new column that you can later select:这将创建一个新列,您稍后可以使用 select:

 df_flights[['flight', 'seat_count']]
 flight seat_count 0 f1 8 1 f2 10 2 f3 12

Here's a way to do that in a fully vectorized way: melting the dataframe, summing only the relevant columns, and getting the total back into the dataframe:这是一种以完全矢量化的方式执行此操作的方法:融合 dataframe,仅对相关列求和,然后将总数返回 dataframe:

d = dict([[y, x] for x, y in enumerate(df.columns[:-1])])

temp_df = df.copy()
temp_df = temp_df.rename(columns=d)
temp_df = temp_df.reset_index().melt(id_vars = ["index", "iloc_value"])
temp_df = temp_df[temp_df.variable <= temp_df.iloc_value]
df["total"] = temp_df.groupby("index").value.sum()

The output is: output 是:

   A  B  C  D  iloc_value  total
0  1  2  3  2           1      3
1  1  3  4  2           2      8
2  4  6  3  2           1     10

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM