我有一个数据框,像这样“响应”:
campaign_type contact_id
Email 0031B00002cPLuFQAW
Analyst Report 0031B00002eP5ijQAC
Operational 003a000001nHioCAAS
Telemarketing 0031B00002Z2zMYQAZ
Webinar - Internal 0031300002VRYU1AAP
然后我像这样构建一个聚合的数据框“ aggr_responses”:
aggr_responses = responses.groupby(
['contact_id', 'campaign_type']
).agg(
{'campaign_type':['count']}
)
这将产生一个新的数据帧,如下所示:
contact_id campaign_type campaign_type count
00313000028gB1tAAE Demo or Trial 1
00313000028yoRWAAY Web Direct 1
...
但是当我尝试基于contact_id访问行时,出现错误:
aggr_responses.loc[aggr_responses['contact_id'] == '00313000028gB1tAAE']
...
KeyError: 'contact_id'
我在做什么错?是否无法通过列值访问汇总数据框?
更新:我提到的是 How to move pandas data from index to column after multiple groupby ,但我认为可能有所不同。
我尝试过:
aggr_responses = pd.read_csv('campaign_responses.csv', encoding='ISO-8859-1').groupby(
['contact_id', 'campaign_type']
).agg(
{'campaign_type':['count']},
as_index=False
)
和
aggr_responses = pd.read_csv('campaign_responses.csv', encoding='ISO-8859-1').groupby(
['contact_id', 'campaign_type'],
as_index=False
).agg(
{'campaign_type':['count']}
)
但是都返回没有索引的数据帧,就像这样:
aggr_responses.head()
contact_id campaign_type campaign_type count
00313000028gB1tAAE Demo or Trial 1
00313000028yoRWAAY Email 1
00313000028yoTSAAY Email 1
00313000028yoUzAAI Email 1
00313000028yqtSAAQ Email 2
而且我仍然无法通过contact_id值访问aggr_responses中的行
aggr_responses.loc[aggr_responses['contact_id'] == '00313000028gB1tAAE']
...
KeyError: 'contact_id'
...