我有一个pandas数据框:
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'Start': [0, 71, 0, 0, 12, 56],
'End': [70, 88, 10, 11, 55, 90],
'Value': [1, 0, 1, 1, 0, 1],
'Name': ['A','A', 'B','C','C','C']},
index =[0, 1, 2, 3, 4, 5])
每个名字('A','B','C') 我想计算一些操作...
如何以更加pythonic的方式执行以下操作?
df3 = pd.DataFrame(columns=['value1','value2','value3'])
unique_names = list(df1['Name'].unique())
for name in unique_names:
df=df1.loc[df1['Name'] == name]
value1 = operation1(df)
value2 = operation2(df)
value3 = operation3(df)
df_temp = pd.DataFrame(np.array([value1,value2,value3]).reshape(-1,3),columns=['value1','value2','value3'])
df3 = pd.concat([df3, df_temp], ignore_index=True)[df3.columns.tolist()]
答案 0 :(得分:0)
这就是Button
的用武之地。
比如说你的三个操作如下:
Background
你可以在一次手术中准确地实现你所追求的目标:
df.groupby().apply()
返回:
#get sum of all Start values
operation1 = lambda x: x.Start.sum()
#get sum of all End values
operation2 = lambda x: x.End.sum()
#get mean of Values
operation3 = lambda x: x.Value.mean()