df=pd.DataFrame([
[56.7,7068.7], [57.19,7040.4], [60.71,8974.25], [60.875,8933.8],
[60.93,8891.3], [60.955,9479.7], [60.965,8962.35], [61.01,9044.8],
[61.01,8980.35], [61.145,9438.5], [61.28,9441.15], [61.375,9042.5],
[61.45,9374.5], [61.46,9431.6], [61.47,9533.5], [61.5,9427.8]
], columns=['RPM','Power'])
def round_to_value(number,roundto):
return (round(number / roundto) * roundto)
rpmstart=round_to_value(df['RPM'].min(),0.5)
rpmend=round_to_value(df['RPM'].max(),0.5)+1
groups=df.groupby(pd.cut(df['RPM'], np.arange(rpmstart,rpmend,1.0))).mean()
groups['band']=groups.index
df['avg']=pd.cut(df['RPM'], np.arange(rpmstart,rpmend,1.0))
原始问题在这里:Averaging a quantity for a particular RPM band and replacing in Pandas dataframe
基本上,我希望能够平均1 rpm频段的功率,从ablev数据帧中的56.5-57.5开始,并替换数据帧'df'中相关RPM频段的功率值。 我做了这个代码,它部分地做了,我被卡住了。数据帧'组'具有RPM范围和平均功率,'df.avg'也具有RPM频带。如何在同一乐队中将'群组'的力量替换为'df'?你可以帮我解决这个问题吗?
答案 0 :(得分:0)
我已经弄清楚了。这是最终代码,它将找出RPM所在的间隔,并找到该间隔内所有功率的平均功率。
df=pd.DataFrame([
[56.7,7068.7], [57.19,7040.4], [60.71,8974.25], [60.875,8933.8],
[58.5,8250.3], [58.6,8520.7], [58.8,8999.0], [59.2,9020.1],
[60.93,8891.3], [60.955,9479.7], [60.965,8962.35], [61.01,9044.8],
[61.01,8980.35], [61.145,9438.5], [61.28,9441.15], [61.375,9042.5],
[61.45,9374.5], [61.46,9431.6], [61.47,9533.5], [61.5,9427.8],[59.6,8920]
], columns=['RPM','Power'])
def round_to_value(number,roundto):
return (round(number / roundto) * roundto)
rpmstart=round_to_value(df['RPM'].min(),0.5)
rpmend=round_to_value(df['RPM'].max(),0.5)+1
groups=df.groupby(pd.cut(df['RPM'], np.arange(rpmstart,rpmend,1.0))).mean()
groups['band']=groups.index
glen=range(0,len(groups))
groups['in']=glen
groups=groups.set_index('in')
df['band']=pd.cut(df['RPM'], np.arange(rpmstart,rpmend,1.0))
df3=pd.merge(df,groups, on=['band'], how='inner')
df3=df3.rename (columns={'Power_x':'Avg_Shaft_Power',
'Power_y':'RPM_Avg_Power', 'RPM_x':'Avg_Shaft_Speed'})
df3.drop(['RPM_y'], axis=1, inplace=True)
谢谢。
此致 阿伦