Min*_*Mai 6 python dataframe pandas
我有一个这样的 Python 数据框,只有一列:
index Train_station
0 Adenauerplatz 52° 29? 59? N, 13° 18? 26? O
1 Afrikanische Straße 52° 33? 38? N, 13° 20? 3? O
2 Alexanderplatz 52° 31? 17? N, 13° 24? 48? O
Run Code Online (Sandbox Code Playgroud)
我想把它分成 3 列:火车站、纬度、经度。数据框应如下所示:
index Train_station Latitude Longitude
0 Adenauerplatz 52° 29? 59? N 13° 18? 26? O
1 Afrikanische Straße 52° 33? 38? N 13° 20? 3? O
2 Alexanderplatz 52° 31? 17? N 13° 24? 48? O
Run Code Online (Sandbox Code Playgroud)
我试过使用df[['Latitude', 'Longitude']] = df.Train_station.str.split(',', expand=True)但它只在纬度和经度坐标之间拆分。如何拆分具有多个我定义的条件的列?
我想过从左边开始检查字符串的方法,然后在它遇到整数或定义的字符串时拆分它,但到目前为止我还没有找到这个方法的答案。
df = df.Train_station.str.split(r'(.*?)(\d+°[^,]+),(.*)', expand=True)
print(df.loc[:, 1:3].rename(columns={1:'Train_station', 2:'Latitude', 3:'Longitude'}) )
Run Code Online (Sandbox Code Playgroud)
印刷:
Train_station Latitude Longitude
0 Adenauerplatz 52° 29? 59? N 13° 18? 26? O
1 Afrikanische Straße 52° 33? 38? N 13° 20? 3? O
2 Alexanderplatz 52° 31? 17? N 13° 24? 48? O
Run Code Online (Sandbox Code Playgroud)
编辑:谢谢@ALollz,您可以使用str.extract():
df = df.Train_station.str.extract(r'(?P<Train_station>.*?)(?P<Latitude>\d+°[^,]+),(?P<Longitude>.*)', expand=True)
print(df)
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以使用该.split()方法来分隔字符串中的值。
使用.apply()为每个所需的列名创建新的数据帧列。
import pandas as pd
data = ["Adenauerplatz 52° 29? 59? N, 13° 18? 26? O",
"Afrikanische Straße 52° 33? 38? N, 13° 20? 3? O",
"Alexanderplatz 52° 31? 17? N, 13° 24? 48? O"]
df = pd.DataFrame(data, columns=['Train_station'])
def train_station(x):
x = x.split(' ', 1)
return x[0]
def latitude(x):
x = x.split(' ', 1)
x = x[1].split(', ', 1)
return x[0]
def longitude(x):
x = x.split(' ', 1)
x = x[1].split(', ', 1)
return x[1]
df['Latitude'] = df['Train_station'].apply(latitude)
df['Longitude'] = df['Train_station'].apply(longitude)
df['Train_station'] = df['Train_station'].apply(train_station)
print(df)
Run Code Online (Sandbox Code Playgroud)
您在上面看到的是对原始数据框的重新创建,然后使用.split()和修改.apply()
输出:
Train_station Latitude Longitude
0 Adenauerplatz 52° 29? 59? N 13° 18? 26? O
1 Afrikanische Straße 52° 33? 38? N 13° 20? 3? O
2 Alexanderplatz 52° 31? 17? N 13° 24? 48? O
Run Code Online (Sandbox Code Playgroud)
你可以尝试这样的事情:
\ndf['Latitude']=df['Train_station'].apply(lambda x: ' '.join([i for i in x.split(' ') if any((lett.replace(',','') in '\xc2\xb0\xe2\x80\xb2\xe2\x80\xb3') for lett in i)]).split(',')[0])\ndf['Longitude']=df['Train_station'].apply(lambda x: ' '.join([i for i in x.split(' ') if any((lett.replace(',','') in '\xc2\xb0\xe2\x80\xb2\xe2\x80\xb3O') for lett in i)]).split(',')[1])\ndf['Train_station']=df['Train_station'].apply(lambda x: ''.join([i for i in x.split(' ') if not any((lett.replace(',','') in '\xc2\xb0\xe2\x80\xb2\xe2\x80\xb3') for lett in i) ]))\nRun Code Online (Sandbox Code Playgroud)\n输出:
\n Train_station Latitude Longitude\n0 Adenauerplatz 52\xc2\xb0 29\xe2\x80\xb2 59\xe2\x80\xb3 N 13\xc2\xb0 18\xe2\x80\xb2 26\xe2\x80\xb3 O\n1 Afrikanische Stra\xc3\x9fe 52\xc2\xb0 33\xe2\x80\xb2 38\xe2\x80\xb3 N 13\xc2\xb0 20\xe2\x80\xb2 3\xe2\x80\xb3 O\n2 Alexanderplatz 52\xc2\xb0 31\xe2\x80\xb2 17\xe2\x80\xb3 N 13\xc2\xb0 24\xe2\x80\xb2 48\xe2\x80\xb3 O\nRun Code Online (Sandbox Code Playgroud)\n