根据 pandas 中的条件创建虚拟变量
Creating a dummy varuable based on a criteria in pandas
我有这样一个数据框:
date sales company country
16/03/2012 3000 H&M US
13/04/2012 2300 H&M US
26/03/2012 1230 H&M FR
13/04/2012 1300 H&M FR
23/03/2012 2230 H&M IT
19/04/2012 1100 H&M IT
16/03/2012 3000 ABC US
13/04/2012 2300 ABC US
26/03/2012 100 ABC FR
13/04/2012 60 ABC FR
23/03/2012 435 ABC IT
19/04/2012 300 ABC IT
我想定义一个标准:如果一家公司的(平均)销售额少于 50% 来自单一国家/地区,则该公司被视为国际公司。我想创建一个新列,如果它是国际的,它的值为 1,否则为 0。最终输出应如下所示:
date sales company country international
16/03/2012 3000 H&M US 1
13/04/2012 2300 H&M US 1
26/03/2012 1230 H&M FR 1
13/04/2012 1300 H&M FR 1
23/03/2012 2230 H&M IT 1
19/04/2012 1100 H&M IT 1
16/03/2012 3000 ABC US 0
13/04/2012 2300 ABC US 0
26/03/2012 100 ABC FR 0
13/04/2012 60 ABC FR 0
23/03/2012 435 ABC IT 0
19/04/2012 300 ABC IT 0
我怎么做到的?
P.S: 数据集中可能缺少销售值,我怎么能说忽略这些值呢?
使用transform
作为手段,comapre他们和groupby与聚合all
检查是否所有值return True
s:
s2 = df.groupby('company')['sales'].transform('mean') / 2
print (s2)
0 930.00
1 930.00
2 930.00
3 930.00
4 930.00
5 930.00
6 516.25
7 516.25
8 516.25
9 516.25
10 516.25
11 516.25
Name: sales, dtype: float64
s1 = df.groupby(['company', 'country'])['sales'].transform('mean')
print (s1)
0 2650.0
1 2650.0
2 1265.0
3 1265.0
4 1665.0
5 1665.0
6 2650.0
7 2650.0
8 80.0
9 80.0
10 367.5
11 367.5
Name: sales, dtype: float64
df['international'] = (s1 > s2).groupby(df['company']).transform('all').astype(int)
print (df)
date sales company country international
0 16/03/2012 3000 H&M US 1
1 13/04/2012 2300 H&M US 1
2 26/03/2012 1230 H&M FR 1
3 13/04/2012 1300 H&M FR 1
4 23/03/2012 2230 H&M IT 1
5 19/04/2012 1100 H&M IT 1
6 16/03/2012 3000 ABC US 0
7 13/04/2012 2300 ABC US 0
8 26/03/2012 100 ABC FR 0
9 13/04/2012 60 ABC FR 0
10 23/03/2012 435 ABC IT 0
11 19/04/2012 300 ABC IT 0
我有这样一个数据框:
date sales company country
16/03/2012 3000 H&M US
13/04/2012 2300 H&M US
26/03/2012 1230 H&M FR
13/04/2012 1300 H&M FR
23/03/2012 2230 H&M IT
19/04/2012 1100 H&M IT
16/03/2012 3000 ABC US
13/04/2012 2300 ABC US
26/03/2012 100 ABC FR
13/04/2012 60 ABC FR
23/03/2012 435 ABC IT
19/04/2012 300 ABC IT
我想定义一个标准:如果一家公司的(平均)销售额少于 50% 来自单一国家/地区,则该公司被视为国际公司。我想创建一个新列,如果它是国际的,它的值为 1,否则为 0。最终输出应如下所示:
date sales company country international
16/03/2012 3000 H&M US 1
13/04/2012 2300 H&M US 1
26/03/2012 1230 H&M FR 1
13/04/2012 1300 H&M FR 1
23/03/2012 2230 H&M IT 1
19/04/2012 1100 H&M IT 1
16/03/2012 3000 ABC US 0
13/04/2012 2300 ABC US 0
26/03/2012 100 ABC FR 0
13/04/2012 60 ABC FR 0
23/03/2012 435 ABC IT 0
19/04/2012 300 ABC IT 0
我怎么做到的? P.S: 数据集中可能缺少销售值,我怎么能说忽略这些值呢?
使用transform
作为手段,comapre他们和groupby与聚合all
检查是否所有值return True
s:
s2 = df.groupby('company')['sales'].transform('mean') / 2
print (s2)
0 930.00
1 930.00
2 930.00
3 930.00
4 930.00
5 930.00
6 516.25
7 516.25
8 516.25
9 516.25
10 516.25
11 516.25
Name: sales, dtype: float64
s1 = df.groupby(['company', 'country'])['sales'].transform('mean')
print (s1)
0 2650.0
1 2650.0
2 1265.0
3 1265.0
4 1665.0
5 1665.0
6 2650.0
7 2650.0
8 80.0
9 80.0
10 367.5
11 367.5
Name: sales, dtype: float64
df['international'] = (s1 > s2).groupby(df['company']).transform('all').astype(int)
print (df)
date sales company country international
0 16/03/2012 3000 H&M US 1
1 13/04/2012 2300 H&M US 1
2 26/03/2012 1230 H&M FR 1
3 13/04/2012 1300 H&M FR 1
4 23/03/2012 2230 H&M IT 1
5 19/04/2012 1100 H&M IT 1
6 16/03/2012 3000 ABC US 0
7 13/04/2012 2300 ABC US 0
8 26/03/2012 100 ABC FR 0
9 13/04/2012 60 ABC FR 0
10 23/03/2012 435 ABC IT 0
11 19/04/2012 300 ABC IT 0