您可以使用.loc
和列名称,如下所示:
import pandas as pd
import numpy as np
np.random.seed(12)
df = pd.DataFrame(
{
"df0" : np.random.choice(["a", "b"], 100),
"df1" : np.random.randint(0, 15, 100),
"df2" : np.random.randint(0, 15, 100),
"df3" : np.random.randint(0, 15, 100),
"df4" : np.random.randint(0, 15, 100),
}
)
print(df.head())
l = [2, 3, 1, 4]
df.loc[:, ["df1", "df2", "df3", "df4"]] *= np.array(l)
df.head()
这是输出:
df0 df1 df2 df3 df4
0 b 5 10 7 13
1 b 3 2 13 3
2 a 5 0 11 14
3 b 11 1 7 10
4 b 0 4 1 12
df0 df1 df2 df3 df4
0 b 10 30 7 52
1 b 6 6 13 12
2 a 10 0 11 56
3 b 22 3 7 40
4 b 0 12 1 48