【问题标题】:Keyword Detection in CSV and add to new columnCSV 中的关键字检测并添加到新列
【发布时间】: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 上读/写。

提前致谢!

编辑: 我尝试了以下线程之类的方法,但我得到了错误,我不知道出了什么问题,所以我愿意接受新的想法。

How to append a new column to a CSV file using Python?

Adding new column to CSV in Python

【问题讨论】:

  • 你会如何分类例如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。您现在所处的位置越具体,我们就能越好地帮助您

标签: python pandas csv


【解决方案1】:

如果你有这个数据名:

        Date     Record_Type                                    Location Proterty_Type   Price
0  19-Mar-21  Active Listing              Sky Tower Downtown Los Angeles     Apartment   15000
1  19-Mar-21  Active Listing  Central Park Residential Tower, 5th Avenue     Apartment   17000
2  20-Mar-21  Active Listing                   Meadow Gardens, Park View         Villa  125000

然后:

keywords = ["Downtown", "Park View", "Industrial District"]

df.insert(
    loc=3,
    column="Area",
    value=df["Location"].apply(
        lambda x: next((kw for kw in keywords if kw in x), "Other")
    ),
)
print(df)

Location 旁边创建Area 列并打印:

        Date     Record_Type                                    Location       Area Proterty_Type   Price
0  19-Mar-21  Active Listing              Sky Tower Downtown Los Angeles   Downtown     Apartment   15000
1  19-Mar-21  Active Listing  Central Park Residential Tower, 5th Avenue      Other     Apartment   17000
2  20-Mar-21  Active Listing                   Meadow Gardens, Park View  Park View         Villa  125000

【讨论】:

  • 完美运行,谢谢!,有什么地方可以推荐我查看lambda 的文档吗?
  • @MahmoudAlQadi lambda 是标准 Python 语法,您可以从官方 Python 文档开始:docs.python.org/3/tutorial/controlflow.html#lambda-expressions
  • 在单独的日期集上尝试这个,我收到一个错误TypeError: argument of type 'float' is not iterable 可能是什么原因造成的?我怎样才能让它跳过花车?
  • @MahmoudAlQadi 不应该发生,您的列是string 类型的吗?你可以在df['Location'] = df['Location'].astype(str)之前做
  • 它们是字符串,但由于值太多,我没有手动检查所有内容。您的修复工作,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-30
  • 1970-01-01
  • 2013-02-24
  • 2019-08-22
  • 2019-11-19
  • 2013-05-02
  • 1970-01-01
相关资源
最近更新 更多