2017-08-30 234 views
0
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轉帶,從56.5-開始57.5在有效數據幀中,並替換數據幀'df'中相關RPM波段的功率值。 我做了這個代碼,它部分做到了,我被卡住了。 Dataframe'組'具有RPM範圍和平均功率,'df.avg'也具有RPM帶。我怎樣才能將'羣組'的力量替換成同一樂隊的'df'?你能幫我解決嗎?

回答

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) 

謝謝。

Regards Arun

相關問題