【发布时间】:2021-11-04 04:30:09
【问题描述】:
我目前正在尝试破解一个编程难题,该难题具有非常简单的数据框host,其中有两列名为city 和amenities(均为object 数据类型)。现在,两列中的条目可以重复多次。以下是host 的前几个条目在下方
City Amenities Price($)
NYC {TV,"Wireless Internet", "Air conditioning","Smoke 8
detector",Essentials,"Lock on bedroom door"}
LA {"Wireless Internet",Kitchen,Washer,Dryer,"First aid
kit",Essentials,"Hair dryer","translation missing:
en.hosting_amenity_49","translation missing:
en.hosting_amenity_50"}
10
SF {TV,"Cable TV",Internet,"Wireless Internet",Kitchen,"Free
parking on premises","Pets live on this
property",Dog(s),"Indoor fireplace","Buzzer/wireless
intercom",Heating,Washer,Dryer,"Smoke detector","Carbon
monoxide detector","First aid kit","Safety card","Fire e
extinguisher",Essentials,Shampoo,"24-hour check-
in",Hangers,"Hair dryer",Iron,"Laptop friendly
workspace","translation missing:
en.hosting_amenity_49","translation missing:
en.hosting_amenity_50","Self Check-In",Lockbox} 15
NYC {"Wireless Internet","Air
conditioning",Kitchen,Heating,"Suitable for events","Smoke
detector","Carbon monoxide detector","First aid kit","Fire
extinguisher",Essentials,Shampoo,"Lock on bedroom
door",Hangers,"translation missing:
en.hosting_amenity_49","translation missing:
en.hosting_amenity_50"} 20
LA {TV,Internet,"Wireless Internet","Air
conditioning",Kitchen,"Free parking on
premises",Essentials,Shampoo,"translation missing:
en.hosting_amenity_49","translation missing:
en.hosting_amenity_50"}
LA {TV,"Cable TV",Internet,"Wireless Internet",Pool,Kitchen,"Free
parking on premises",Gym,Breakfast,"Hot tub","Indoor
fireplace",Heating,"Family/kid friendly",Washer,Dryer,"Smoke
detector","Carbon monoxide detector",Essentials,Shampoo,"Lock
on bedroom door",Hangers,"Private entrance"} 28
.....
问题。输出设施数量最多的城市。
我的尝试。我尝试使用groupby() 函数根据city 使用host.groupby('city'). 列对其进行分组现在,我需要计数成功的数量每套便利设施中的元素。由于数据类型不同,len() 函数不起作用,因为集合中的每个元素之间都有\(例如,如果我使用host['amenities'][0],,则输出为"{TV,\"Wireless Internet\",\"Air conditioning\",\"Smoke detector\",\"Carbon monoxide detector\",Essentials,\"Lock on bedroom door\",Hangers,Iron}"。将len() 应用于此输出会导致 134,这显然是不正确的)。我尝试使用 host['amenities'][0].strip('\n') 删除 \, 但 len() 函数仍然给出 134.
谁能帮我解决这个问题?
我的解决方案,灵感来自 ddejohn 的解决方案:
### Transform each "string-type" entry in column "amenities" to "list" type
host["amenities"] = host["amenities"].str.replace('["{}]', "", regex=True).str.split(",")
## Create a new column that count all the amenities for each row
entry host["am_count"] = [len(data) for data in host["amenities"]]
## Output the index in the new column resulting from aggregation over the column `am_count` grouped by `city`
host.groupby("city")["am_count"].agg("sum").argmax()
【问题讨论】:
-
Amenities列是否包含格式奇怪的字符串?还是它实际上包含set?如果它是一个字符串,你能写出接受其中一个字符串并计算便利设施的代码吗? -
我不确定。根据输出,它是一个字符串,但我被困在你要求我做的那个确切的问题上;)
-
我束手无策。抱歉,我没能提供帮助。
-
作为最后的手段,您能否共享您的数据源?这看起来可能像 AirBnB 数据之类的?是否有公共 API 或者我可以从某个地方获取这些数据并在明天搞砸?我要去睡觉了。
-
你是对的,这是来自 AirBnB 的练习面试问题。我在这里找到它:platform.stratascratch.com/coding/…。先生睡个好觉!非常感谢您的帮助。
标签: python-3.x dataframe pandas-groupby