1)通过提供axis = 1来执行w.r.t列的groupby.根据Per @ Boud的评论,你可以通过分组数组中的小调整得到你想要的结果:
df.groupby((np.arange(len(df.columns)) // 2) + 1, axis=1).sum().add_prefix(‘s’)
根据这种情况进行分组:
np.arange(len(df.columns)) // 2
array([0, 0, 1, 1, 2, 2], dtype=int32)
df = pd.DataFrame(np.add.reduceat(df.values, np.arange(len(df.columns))[::2], axis=1))
df.columns = df.columns + 1
df.add_prefix(‘s’)
时间限制:
对于横跨20列的100万行DF:
from string import ascii_lowercase
np.random.seed(42)
df = pd.DataFrame(np.random.randint(0, 10, (10**6,20)), columns=list(ascii_lowercase[:20]))
df.shape
(1000000, 20)
def with_
Original: https://blog.csdn.net/weixin_39761491/article/details/114409159
Author: weixin_39761491
Title: python两列数据相加_python数据框中每列相加_python – Pandas数据帧中每两列的总和…
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/740416/
转载文章受原作者版权保护。转载请注明原作者出处!