英文:
splitting the first number from a string into another column in pandas
问题
我有一个包含房产信息的数据框。目前,房产类型列显示为“3卧室公寓”等。我想将卧室数量和房产类型分别放入两个单独的列中 - 卧室数量"3"和房产类型"公寓",然后删除"卧室"。
我尝试过:
df['卧室数量'] = df['房产'].str[:1]
(其中"房产"是包含相关信息的列的名称)。但是目前我没有得到任何结果。
我该如何解决这个问题?
英文:
I have a dataframe with property information. Currently the property type column reads "3 bedroom apartment" etc. I would like put the number of bedrooms and property type into two separate columns - bedroom_no "3" and property_type "apartment", and drop "bedroom".
I have tried: """ df['bedroom_no'] = df['property'].str[:1] """ (property being the name of the column which contains the relevant information. But I am currently getting no result.
How would I solve this?
答案1
得分: 0
Pandas拥有大量出色的字符串处理方法。对于您提供的具体示例,您可以使用.str.extract()
来获取整数,使用.str.split()
来获取房屋类型。如果您的property
列包含更多信息或信息的顺序不同,请使用具体而代表性的示例更新您的问题。
df = pd.DataFrame({'property': ['3 bedroom apartment', '5 bedroom house']})
df['bedroom_no'] = df['property'].str.extract('(\d+)')
df['property_type'] = df['property'].str.split(' ').str.get(2)
print(df)
控制台输出:
property bedroom_no property_type
0 3 bedroom apartment 3 apartment
1 5 bedroom house 5 house
英文:
Pandas has a ton of great string methods. For the specific example you give, you can use .str.extract()
to get the integer and .str.split()
to get the housing type. If your property
column contains more info or info in a different order, then update your question with specific and representative examples.
df = pd.DataFrame({'property': ['3 bedroom apartment', '5 bedroom house']})
df['bedroom_no'] = df['property'].str.extract('(\d+)')
df['property_type'] = df['property'].str.split(' ').str.get(2)
print(df)
Console output:
property bedroom_no property_type
0 3 bedroom apartment 3 apartment
1 5 bedroom house 5 house
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论