python - For Loop With Pandas Groupby 错误消息:ValueError: Grouper and axis must be the same length
问题描述
当我运行以下代码时,我收到此错误消息: ValueError: Grouper and axis must be same length有人可以帮我理解为什么会这样吗?我浏览了其他一些相关的帖子,但我仍然不明白为什么会收到此错误消息。 在 Python 中,Grouper 和轴的长度必须相同
for (community,label,lp) in zip(communities,event_labels,landing_pages):
lp_data_simple.loc[lp_data_simple['Event Label'].str.contains(
label, regex=True) & lp_data_simple['Landing Page'].str.contains(
lp, regex=True) ,].groupby([(lp_data_simple.index.year),(lp_data_simple.index.month),
'Event Label','Landing Page','Source / Medium','Event Action']).sum()
我有以下三个列表:
communities = ['Firethorne','Glendale Lakes','Kendall Lakes','Kingdom Heights','Lago Mar','Lake Shore','Rodeo Palms',
'Cypress Oaks','Fall Creek','Plantation Lakes','Sunset Ridge','Townsen Landing','Woodridge Forest']
event_labels = ['firethorne|Firethorne','glendale|Glendale','kendall|Kendall','kingdom|Kingdom','lago|Lago',
'lake shore|Lake Shore','rodeo|Rodeo','cypress|Cypress','fall|Fall','plantation|Plantation',
'sunset|Sunset','townsen|Townsen','woodridge|Woodridge']
landing_pages = ['firethorne|not set','glendale|not set','kendall-lakes|not set','kingdom-heights|not set',
'lago-mar|not set','lake-shore|not set','rodeo-palms|not set','cypress-oaks|not set',
'fall-creek|not set','plantation-lakes|not set','sunset-ridge|not set','townsen-landing|not set',
'woodridge-forest|not set']
我有一个像这样的数据框:
Source / Medium \
Date
2018-10-16 (direct) / (none)
2018-10-16 zillow/paid_network / newhomefeed
2018-10-17 (direct) / (none)
2018-10-17 prospecting / email
2018-10-17 prospecting / email
Landing Page Event Category \
Date
2018-10-16 /lp-new/sunset-ridge/ KPI
2018-10-16 /lp/cypress-oaks/zillow/?nhf_channel=6&nhf_lis... KPI
2018-10-17 /lp/woodridge-forest/nhd/ KPI
2018-10-17 /lp/woodridge-forest/email/?cid=18101714470088... KPI
2018-10-17 /lp/woodridge-forest/email/?cid=18101714470088... KPI
Event Action \
Date
2018-10-16 Click to Call
2018-10-16 Click to Call
2018-10-17 Driving Directions
2018-10-17 Driving Directions
2018-10-17 Driving Directions
Event Label Total Events \
Date
2018-10-16 Sunset Ridge (Website) - Click to Call (832.22... 1
2018-10-16 Cypress Oaks (Zillow) - Click to Call (832.220... 3
2018-10-17 Woodridge Forest (New Homes Directory) - Get D... 1
2018-10-17 Woodridge Forest (SmartDirectory Email) - Get ... 1
2018-10-17 Woodridge Forest (SmartDirectory Email) - Get ... 2
Unique Events
Date
2018-10-16 1
2018-10-16 1
2018-10-17 1
2018-10-17 1
2018-10-17 1
我的最终目标是遍历这些列表并提取每个社区所需的相关数据,并将其分组类似于下面的内容。
Total Events \
Date Date Source / Medium Event Action
2018 10 (direct) / (none) Click to Call 3
Driving Directions 2
RDC_NAV / Banner Click to Call 2
Driving Directions 5
classifiedads.com / referral Driving Directions 1
google / cpc Click to Call 10
Driving Directions 17
l.facebook.com / referral Click to Call 1
m.facebook.com / referral Click to Call 3
Driving Directions 4
msn.com / referral Driving Directions 1
newhomesdirectory / referral Driving Directions 1
newhomesdirectory.com / referral Click to Call 6
Driving Directions 10
newhomesource.com / referral Click to Call 3
Driving Directions 2
prospecting / email Click to Call 1
Driving Directions 6
yahoo / organic Driving Directions 1
zillow/paid_network / newhomefeed Click to Call 4
Driving Directions 2
11 (direct) / (none) Click to Call 1
Driving Directions 12
Hotonhomes.com / Neighborhood Watch Email Driving Directions 1
Hotonhomes.com / Right Time Response Email Driving Directions 1
RDC_NAV / Banner Click to Call 1
Driving Directions 2
SmartTouch GEO / geotargeting Driving Directions 2
bing / organic Driving Directions 1
google / cpc Click to Call 51
更新 我能够使用以下 for 循环完成任务
for (community,label,lp) in zip(communities,event_labels,landing_pages):
temp_df = lp_data_simple.loc[lp_data_simple['Event Label'].str.contains(
label, regex=True) & lp_data_simple['Landing Page'].str.contains(
lp, regex=True) ,]
temp_df.groupby([(temp_df.index.year),(temp_df.index.month),'Source / Medium','Event Action']).sum().to_csv(community+'.csv')
解决方案
推荐阅读
- excel - 基于单元格编号的 VBA 复制行,非整数出现错误
- java - 即使方法中没有 MDC.put(),MDC 日志记录如何工作?
- c# - C# OpenXML - 生成的 xlsx 文件即使从基本另存为操作也损坏
- javascript - JS 等待一个函数,直到它接收到数据
- python - 在单元格中使用 python 列表过滤 pandas.DataFrame
- mysql - 在 MySQL Workbench 中导入自包含文件时出现 ERROR 1064
- android - Android 谷歌地图上丢失的瓷砖
- algorithm - 如何将内容动态定位到用户?
- python - 将字符串中的时间段转换为等效的数字天数
- python - conda 升级 python 版本并删除旧版本