【发布时间】:2018-10-11 19:13:20
【问题描述】:
我正在尝试为多个特定列在它们各自的行中设置一个值。
例如:
X Y Z
A 10 1 0 1
B 50 0 0 0
C 80 1 1 1
会变成:
X Y Z
A 10 10 0 10
B 50 0 0 0
C 80 80 80 80
我遇到的问题是使用 mul() 时超时。我的真实数据集非常大。我尝试在我的真实代码中使用循环对其进行迭代,如下所示:
for i in range(1,df_final_small.shape[0]):
df_final_small.iloc[i].values[3:248] = df_final_small.iloc[i].values[3:248] * df_final_small.iloc[i].values[2]
当应用于示例数据框时将如下所示:
for i in range(1,df_final_small.shape[0]):
df_final_small.iloc[i].values[1:4] = df_final_small.iloc[i].values[1:4] * df_final_small.iloc[i].values[0]
必须有更好的方法来做到这一点,我在弄清楚如何只将乘法转换为行中的某些列而不是整行时遇到了问题。
编辑: 在这里更详细的是我的 df.head(5)。
id gross 150413 Welcome Email 150413 Welcome Email Repeat Cust 151001 Welcome Email 151001 Welcome Email Repeat Cust 161116 eKomi 1702 Hot Leads Email 1702 Welcome Email - All Purchases 1804 Hot Leads ... SILVER GOLD PLATINUM Acquisition Direct Mail Conversion Direct Mail Retention Direct Mail Retention eMail cluster x y
0 0033333 46.2 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 1.0 0.0 0.0 0.0 1.0 0.0 10 -0.230876 0.461990
1 0033331 2359.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 ... 0.0 1.0 0.0 0.0 0.0 1.0 0.0 9 0.231935 -0.648713
2 0033332 117.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 ... 0.0 1.0 0.0 0.0 0.0 1.0 0.0 5 -0.812921 -0.139403
3 0033334 89.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 ... 0.0 1.0 0.0 0.0 0.0 1.0 0.0 5 -0.812921 -0.139403
4 0033335 1908.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 1.0 0.0 0.0 1.0 0.0 0.0 7 -0.974142 0.145032
【问题讨论】:
-
索引是
A 50,需要从中提取数字吗? -
你能打印
df.head(5)吗?
标签: python pandas multiplication