你可以先用set_index
列Store
,然後通過sum
div
分最後reset_index
:
df.set_index('Store', inplace=True)
df = df.div(df.sum(1), axis=0)
print (df.reset_index())
Store Meat Veg Bread
0 N 0.740741 0.074074 0.185185
1 S 0.431034 0.172414 0.396552
2 E 0.357143 0.107143 0.535714
3 W 0.689655 0.137931 0.172414
通過ix
或iloc
與選擇另一種解決方案:
df.ix[:,'Meat':] = df.ix[:,'Meat':].div(df.ix[:,'Meat':].sum(1), axis=0)
print (df)
Store Meat Veg Bread
0 N 0.740741 0.074074 0.185185
1 S 0.431034 0.172414 0.396552
2 E 0.357143 0.107143 0.535714
3 W 0.689655 0.137931 0.172414
df.iloc[:,1:] = df.iloc[:,1:].div(df.iloc[:,1:].sum(1), axis=0)
print (df)
Store Meat Veg Bread
0 N 0.740741 0.074074 0.185185
1 S 0.431034 0.172414 0.396552
2 E 0.357143 0.107143 0.535714
3 W 0.689655 0.137931 0.172414
時序:
In [187]: %timeit (jez1(df))
100 loops, best of 3: 4.07 ms per loop
In [188]: %timeit (jez2(df1))
100 loops, best of 3: 5.61 ms per loop
In [189]: %timeit (jez3(df2))
100 loops, best of 3: 5.44 ms per loop
In [190]: %timeit (ric(df3))
100 loops, best of 3: 6.18 ms per loop
In [191]: %timeit (ogi(df4))
1 loop, best of 3: 2.2 s per loop
代碼定時 S:
#random dataframe
np.random.seed(100)
#10 data columns + first Store col, 10k rows
df = pd.DataFrame(np.random.randint(10, size=(10000,10)), columns=list('ABCDEFGHIJ'))
df.index = 'a' + df.index.astype(str)
df = df.reset_index().rename(columns={'index':'Store'})
print (df)
df1, df2, df3, df4 = df.copy(), df.copy(), df.copy(), df.copy()
def jez1(df):
df = df.set_index('Store')
df = 100 * df.div(df.sum(1), axis=0)
return (df.reset_index())
def jez2(df):
df.ix[:,'A':] = df.ix[:,'A':].div(df.ix[:,'A':].sum(1), axis=0)
return df
def jez3(df):
df.iloc[:,1:] = df.iloc[:,1:].div(df.iloc[:,1:].sum(1), axis=0)
return df
def ric(df):
df.ix[:, 1:] = (df.ix[:, 1:].T/df.ix[:, 1:].sum(1)).T
return df
def ogi(df):
df.ix[:, 1:]=df.ix[:,1:].apply(lambda x: x/x.sum(), axis=1)
return df
print (jez1(df))
print (jez2(df1))
print (jez3(df2))
print (ric(df3))
print (ogi(df4))
我更喜歡你的我的。使用'set_index'和'div'比使用索引和轉置更加優雅。 –
@RicardoCruz - 感謝您的評論。 – jezrael