我的数据框中有一列如下所示:
输入
df['location.display_name']
输出
Kelso, Scottish Borders
Manchester, Greater Manchester
Northampton, Northamptonshire
Reading, Berkshire
Leicester, Leicestershire
Newport, Wales
Swindon, Wiltshire
Perth, Perth & Kinross
Manchester, Greater Manchester
Perth, Perth & Kinross
Cardiff
Hull, East Riding Of Yorkshire
Chester, Cheshire
Southampton
Leamington Spa, Warwickshire
Swindon, Wiltshire
Slough, Berkshire
Portsmouth, Hampshire
我想创建一个只包含位置第一部分的新列-例如:Swindon,Wiltshire我想保留Swindon并将其添加到新列中。你知道吗
还有,这会对我想保留的一些单词有什么影响,比如Cardiff
?你知道吗
要在列的每个元素上执行自定义功能,可以使用pandas
apply
函数。在您的情况下,应该使用以下代码:输出如下所示:
我认为需要^{} 按
str[0]
选择第一列list
或按[0]
选择第一列:如果数据中没有
NaN
s和None
s,则可以使用list comprehension
:相关问题 更多 >
编程相关推荐