Python 通过一列将dataframe中的所有值多重化
我有以下数据帧dfPython 通过一列将dataframe中的所有值多重化,python,Python,我有以下数据帧df item 1 item 2 item3 item4 values 1 0 1 1 10 0 1 0 1 22 ... 怎么能得到, item1 item2 item3 item4 10 0 10 10 0 22 0 22 谢谢 试试看: df.ilo
item 1 item 2 item3 item4 values
1 0 1 1 10
0 1 0 1 22
...
怎么能得到,
item1 item2 item3 item4
10 0 10 10
0 22 0 22
谢谢 试试看:
df.iloc[:,0:4] = df.iloc[:,0:4].multiply(df.iloc[:,4].values, axis=0)
例如:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(12).reshape(3,4), columns=list('abcd'))
print(df)
df.iloc[:,0:3] = df.iloc[:,0:3].multiply(df.iloc[:,3].values, axis=0)
print(df)
输出:
a b c d
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
a b c d
0 0 3 6 3
1 28 35 42 7
2 88 99 110 11
如果值是数据帧的一部分,这可能有助于: