gpt4 book ai didi

python - 根据逗号获取数组列中元素的数量,并将该计数转换为自己的列

转载 作者:行者123 更新时间:2023-12-01 00:07:06 26 4
gpt4 key购买 nike

我有一个数据框,当我请求唯一值时,其中一列输出以下内容(我最初考虑在组合较少的情况下手动映射计数):

df.amenities.unique()
array(['{TV,Wifi,Kitchen,Elevator,Heating,Washer,"First aid kit","Fire extinguisher",Essentials,Hangers,"Hair dryer",Iron,"Laptop friendly workspace","Private entrance"}',
'{TV,Wifi,Kitchen,"Free parking on premises","Indoor fireplace",Heating,"Family/kid friendly",Washer,"First aid kit","Fire extinguisher",Essentials,"Lock on bedroom door",Hangers,"Hair dryer",Iron,"Laptop friendly workspace","Private entrance"}'])

为了处理这个设施数组,我决定首先去掉引号:

df['amenities'] = df['amenities'].str.replace('"', '')

我的策略是计算每个数组元素中出现的逗号数量,添加 1 以说明尾部缺少的逗号,并使用 reset_index 来命名我希望在其中显示计数的列。

(df['amenities'].str.count(',').add(1).sum().reset_index(name='amenities_count'))

这不太有效,因为我收到错误:

AttributeError: 'numpy.int64' object has no attribute 'reset_index'

如果可能的话,您能否解释一下为什么这不是一个好的方法以及什么是好的替代方法?

感谢您的宝贵时间。

回应伯纳德:

Dataframe:

Apt Counties amenities
S1 C1 {TV, "Kitchen", "WiFi"}
S1 C1 {"Hair dryer"}
S2 C1 {"Heating", Essentials}
S2 C2 {"Cable", Kitchen, "WiFi"}

Output:

Apt Counties amenities amenities_counts
S1 C1 {TV, "Kitchen", "WiFi"} 3
S1 C1 {"Hair dryer"} 1
S2 C1 {"Heating", Essentials} 2
S2 C2 {"Cable", Kitchen, "WiFi"} 3

最佳答案

作为示例,计算 ',' 加一并将其分配给新列

df['amenities_count'] = df.amenities.str.count(',').add(1)    

Out[1274]:
Apt Counties amenities amenities_count
0 S1 C1 {TV, "Kitchen", "WiFi"} 3
1 S1 C1 {"Hair dryer"} 1
2 S2 C1 {"Heating", Essentials} 2
3 S2 C2 {"Cable", Kitchen, "WiFi"} 3

关于python - 根据逗号获取数组列中元素的数量,并将该计数转换为自己的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59901247/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com