【发布时间】:2021-04-05 21:48:40
【问题描述】:
我在 CSV 文件(近 50 万个条目)中有房地产及其详细信息(17 列)。其中一列提供了一个位置,但实际上有点太详细了。我想对我的条目进行分类,因此我想简化位置以提供更多通用区域。我会将我想要将条目分类到列表中的区域,例如:
keywords = ['Downtown','Park View','Industrial District', ... ]
因此,理想情况下,我想获取一个具有例如 Sky Tower Downtown Los Angeles 的条目,然后将其归类为 Downtown。
所以任务是首先检测location 列中的关键字,然后将其附加到新列(如果可能,就在它旁边)。如果条目中没有找到关键字,我会将其归类为Other。
看起来像这样:
| Date | Record_Type | Location | Proterty_Type | ... | Price |
|---|---|---|---|---|---|
| 19-Mar-21 | Active Listing | Sky Tower Downtown Los Angeles | Apartment | ... | 15000 |
| 19-Mar-21 | Active Listing | Central Park Residential Tower, 5th Avenue | Apartment | ... | 17000 |
| 20-Mar-21 | Active Listing | Meadow Gardens, Park View | Villa | ... | 125000 |
类似于:
| Date | Record_Type | Location | Area | Proterty_Type | ... | Price |
|---|---|---|---|---|---|---|
| 19-Mar-21 | Active Listing | Sky Tower Downtown Los Angeles | Downtown | Apartment | ... | 15000 |
| 19-Mar-21 | Active Listing | Central Park Residential Tower, 5th Avenue | Other | Apartment | ... | 17000 |
| 20-Mar-21 | Active Listing | Meadow Gardens, Park View | Park View | Villa | ... | 125000 |
最后,它将所有内容保存到一个新的 csv 文件中。理想情况下,我还希望您使用pandas 在 csv 上读/写。
提前致谢!
编辑: 我尝试了以下线程之类的方法,但我得到了错误,我不知道出了什么问题,所以我愿意接受新的想法。
【问题讨论】:
-
你会如何分类例如
Meadow Gardens, Park View, Downtown?公园景观还是市中心? -
这太宽泛了。你尝试过什么,你的尝试出了什么问题?例如,pandas 有
read_csv()和Series.str.contains()方法,这似乎是一个不错的起点 -
理想情况下,大多数情况下的字符串中没有多个关键字,但如果我这样做Other,然后我可以稍后手动检查。
-
@G.Anderson 我使用
df = pd.read_csv(r'listings.csv', names=col_names, skiprows=[0])导入数据,col_names来自我在导入前定义的列表。 -
这将是很好的细节edit 你的问题,使其成为minimal reproducible example。您现在所处的位置越具体,我们就能越好地帮助您