【发布时间】:2022-01-05 20:14:33
【问题描述】:
我有一个包含几百万行的数据框。我需要计算从特定列索引到最后一列的每一行的总和。每行的列索引是唯一的。具有所需输出的示例如下:
import pandas as pd
df = pd.DataFrame({'col1': [1, 2, 2, 5, None, 4],
'col2': [4, 2, 4, 2, None, 1],
'col3': [6, 3, 8, 6, None, 4],
'col4': [9, 8, 9, 3, None, 5],
'col5': [1, 3, 0, 1, None, 7],
})
df_ind = pd.DataFrame({'ind': [1, 0, 3, 4, 3, 5]})
for i in df.index.to_list():
df.loc[i, "total"] = df.loc[i][(df_ind.loc[i, "ind"]).astype(int):].sum()
print(df)
>>
col1 col2 col3 col4 col5 total
0 1.0 4.0 6.0 9.0 1.0 20.0
1 2.0 2.0 3.0 8.0 3.0 18.0
2 2.0 4.0 8.0 9.0 0.0 9.0
3 5.0 2.0 6.0 3.0 1.0 1.0
4 NaN NaN NaN NaN NaN 0.0
5 4.0 1.0 4.0 5.0 7.0 0.0
如何在不使用 for 循环的情况下使用 pandas 有效地实现这一点。谢谢
【问题讨论】: