Python 熊猫:如何在多个分隔符上拆分?
我有一个数据框,它在一列(Python 熊猫:如何在多个分隔符上拆分?,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个数据框,它在一列(坐标)中包含纬度、经度和海拔,我想将坐标列拆分为三列(纬度、经度和海拔) df: 预期产出: ID lat lon alt Region 1 52.00755721100514 12.565129548994266 185.23616827199143 Europe 2 52.00755721100514 1
坐标
)中包含纬度、经度和海拔,我想将坐标
列拆分为三列(纬度、经度和海拔)
df:
预期产出:
ID lat lon alt Region
1 52.00755721100514 12.565129548994266 185.23616827199143 Europe
2 52.00755721100514 12.565129548994266 185.23616827199143 Europe
3 52.00755721100514 12.565129548994266 185.23616827199143 Europe
4 52.00755721100514 12.565129548994266 185.23616827199143 Europe
5 52.00755721100514 12.565129548994266 185.23616827199143 Europe
我尝试的是:
我试图首先在:
的基础上拆分列,但它不起作用:
df.loc[df['Coordinates'].isin(["latitude_degrees", "longitude_degrees"])]= ""
df.Coordinates.replace(to_replace=['latitude_degrees','longitude_degrees'],value='')
我还试图替换文本,但它不起作用:
df.loc[df['Coordinates'].isin(["latitude_degrees", "longitude_degrees"])]= ""
df.Coordinates.replace(to_replace=['latitude_degrees','longitude_degrees'],value='')
让我们使用从坐标
列中提取lat
、long
和alt
,然后取消堆叠
对其进行整形,最后将其与列ID
和区域
连接起来:
c = df['Coordinates'].str.extractall(r'([\d.]+)')[0].unstack()
d = df[['ID', 'Region']].join(c.set_axis(['lat', 'long', 'alt'], 1))
@谢谢,但我已经试过这个了,它不起作用了。你试过什么?使用str.split
的答案不在您提供的代码中。@Yukishoriii我尝试了100个答案,但无法添加所有有问题的答案,对吗?添加str.split一个