如果它们在数据框的特定列中共享相同的值,我想汇总某些列中的值吗?
换句话说,如何从A获取数据帧B?在这个例子中,我想根据列c1中的值进行检查,如果有相同值的行,那么我想将列c3-c5的总和放在行中的新列(c6)中,其值为& #34; C"在其专栏c2中。
A:
c0 c1 c2 c3 c4 c5 0 1 a d 3 4 0 1 1 a c 0 0 6 2 1 b d 3 1 0 1 1 b c 0 0 1
B:
c0 c1 c2 c3 c4 c5 c6 0 1 a d 3 4 0 1 1 a c 0 0 6 13 2 1 b d 3 1 0 1 1 b c 0 0 1 5
这就是我所做的:
import pandas as pd
import numpy as np
A = pd.read_excel(file_location)
A['c6'] = pd.Series(0, index=A.index)
A.c6[A['c2'] == 'c'] = A.c5 + A.c4[A.c1 == A.c1.shift(-1)] + A.c3[A.c1 == A.c1.shift(-1)]
B = A
答案 0 :(得分:1)
import pandas as pd
import io
import numpy as np
import datetime
data = """
c0 c1 c2 c3 c4 c5
0 1 a d 3 4 0
1 1 a c 0 0 6
2 1 b d 3 1 0
1 1 b c 0 0 1
"""
df = pd.read_csv(io.StringIO(data), delimiter='\s+')
df2 = pd.DataFrame(df.groupby('c1').sum().ix[:,1:].sum(axis=1), columns=['c6']).reset_index()
df3 = pd.merge(df,df2,on='c1').set_index(df.index)
print(df3)
这将给出:
c0 c1 c2 c3 c4 c5 c6
0 1 a d 3 4 0 13
1 1 a c 0 0 6 13
2 1 b d 3 1 0 5
1 1 b c 0 0 1 5