【发布时间】:2019-12-24 01:38:22
【问题描述】:
我正在尝试使用 Python 从数据框中的文本列生成虚拟变量和分类变量。想象一下名为“Cars_listing”的数据框中的文本列“Cars_notes”:
- "This Audi has ABS braking, leather interior and bucket seats..."
- "The Ford F150 is one tough pickup truck, with 4x4, new suspension and club cab..."
- "Our Nissan Sentra comes with ABS brakes, Bluetooth-enabled radio..."
- "This Toyota Corolla is a gem, with new tires, low miles, a few scratches..."
- "The Renault Le Car has been sitting in the garage, a little rust..."
- "The Kia Sorento for sale has a CD player, new tires..."
- "Red Dodge Viper convertible for sale, ceramic brakes, low miles..."
如何制作新变量:
- car_type: American [Ford] (1), European [Audi, Renault] (2), Asian [Toyota, Kia] (3)
- ABS_brakes: description includes 'ABS brak' (1), or not (0)
- imperfection: description includes 'rust' or 'scratches' (1) or not (0)
- sporty: description includes 'convertible' (1) or not (0)
我已经开始尝试re.search()(不是re.match()),比如:
sporty = re.search("convertible",'Cars_notes')
我刚刚开始学习 Python 文本操作和 NLP。我已经在这里搜索了信息以及其他来源(Data Camp、Udemy、Google 搜索),但我还没有找到解释如何操作文本来创建此类分类或虚拟变量的内容。帮助将不胜感激。谢谢!
【问题讨论】:
标签: python string pandas variables text