如何使用另一列的字典计数创建 pandas 列?
How to create a pandas colum with dictionary count from another column?
我有一个示例数据:
date product campaign_type total_monthly_sale
2019-01-01. Dell [call, email, call]. 5
2019-01-01. Apple [fax, fax, visit, visit] 4
2019-02-01. Dell [call, fax, call] 6
2019-02-01. Apple [email, email, visit] 7
我想创建一个名为 'campaign_dict' 的新列,它给出了 campaign_type 列中值的计数。
示例输出如下:
date product campaign_type total_monthly_sale. campaign_dict
2019-01-01. Dell [call, email, call]. 5. {'call':2,'email':1}
2019-01-01. Apple. [fax, fax, visit, visit]. 4. {'fax':2,'visit':2}
2019-02-01. Dell. [call, fax, call] 6. {'call':2,'fax':1}
2019-02-01. Apple. [email, email, visit]. 7 {'email':2,'visit':1}
尝试这样的事情
from collections import Counter
df["campaign_dict"] = df["campaign_type"].apply(Counter)
你需要的是collections.Counter
import pandas as pd
from collections import Counter
df: pd.DataFrame = pd.DataFrame([
[["call", "email", "call"], 5],
[["fax", "fax", "visit", "visit"], 4],
[["call", "fax", "call"], 6],
[["email", "email", "visit"], 7]
], columns=['campaign_type', 'total_monthly_sale'])
df['campaign_dict'] = df['campaign_type'].apply(Counter)
print(df)
campaign_type total_monthly_sale campaign_dict
0 [call, email, call] 5 {'call': 2, 'email': 1}
1 [fax, fax, visit, visit] 4 {'fax': 2, 'visit': 2}
2 [call, fax, call] 6 {'call': 2, 'fax': 1}
3 [email, email, visit] 7 {'email': 2, 'visit': 1}
我有一个示例数据:
date product campaign_type total_monthly_sale
2019-01-01. Dell [call, email, call]. 5
2019-01-01. Apple [fax, fax, visit, visit] 4
2019-02-01. Dell [call, fax, call] 6
2019-02-01. Apple [email, email, visit] 7
我想创建一个名为 'campaign_dict' 的新列,它给出了 campaign_type 列中值的计数。
示例输出如下:
date product campaign_type total_monthly_sale. campaign_dict
2019-01-01. Dell [call, email, call]. 5. {'call':2,'email':1}
2019-01-01. Apple. [fax, fax, visit, visit]. 4. {'fax':2,'visit':2}
2019-02-01. Dell. [call, fax, call] 6. {'call':2,'fax':1}
2019-02-01. Apple. [email, email, visit]. 7 {'email':2,'visit':1}
尝试这样的事情
from collections import Counter
df["campaign_dict"] = df["campaign_type"].apply(Counter)
你需要的是collections.Counter
import pandas as pd
from collections import Counter
df: pd.DataFrame = pd.DataFrame([
[["call", "email", "call"], 5],
[["fax", "fax", "visit", "visit"], 4],
[["call", "fax", "call"], 6],
[["email", "email", "visit"], 7]
], columns=['campaign_type', 'total_monthly_sale'])
df['campaign_dict'] = df['campaign_type'].apply(Counter)
print(df)
campaign_type total_monthly_sale campaign_dict
0 [call, email, call] 5 {'call': 2, 'email': 1}
1 [fax, fax, visit, visit] 4 {'fax': 2, 'visit': 2}
2 [call, fax, call] 6 {'call': 2, 'fax': 1}
3 [email, email, visit] 7 {'email': 2, 'visit': 1}