Saya ingin membagi data berikut menjadi dua kolom, latitude dan longitude dan memasukkannya ke dalam bingkai data.

0     (45.349586099999996, -75.81031967988278)
1            (-37.77922725, 175.2010323246593)
2                   (-42.9945669, 170.7100413)
3                    (-39.2711067, 174.154795)
4                      (51.2800275, 1.0802533)
5           (-41.30222105, 172.89453190955697)
6                   (-35.3712702, 173.7405337)
7                   (-45.7255555, 168.2936808)
8                   (-40.3284102, 175.8190684)
9                   (-45.1299859, 169.5248818)
10           (-37.9503756, 176.93828736155422)

Adakah yang bisa membantu saya?

0
user86907 3 Juni 2021, 02:48

2 jawaban

Jawaban Terbaik

Cara lain:

data='''a  b
0     (45.349586099999996, -75.81031967988278)
1            (-37.77922725, 175.2010323246593)
2                   (-42.9945669, 170.7100413)
3                    (-39.2711067, 174.154795)
4                      (51.2800275, 1.0802533)
5           (-41.30222105, 172.89453190955697)
6                   (-35.3712702, 173.7405337)
7                   (-45.7255555, 168.2936808)
8                   (-40.3284102, 175.8190684)
9                   (-45.1299859, 169.5248818)
10           (-37.9503756, 176.93828736155422)'''        
df = pd.read_csv(io.StringIO(data), sep=' \s+', engine='python')
df[['lat', 'lon']] = df.b.str[1:-1].str.split(',', expand=True)

     a                                         b                 lat                  lon
0    0  (45.349586099999996, -75.81031967988278)  45.349586099999996   -75.81031967988278
1    1         (-37.77922725, 175.2010323246593)        -37.77922725    175.2010323246593
2    2                (-42.9945669, 170.7100413)         -42.9945669          170.7100413
3    3                 (-39.2711067, 174.154795)         -39.2711067           174.154795
4    4                   (51.2800275, 1.0802533)          51.2800275            1.0802533
5    5        (-41.30222105, 172.89453190955697)        -41.30222105   172.89453190955697
6    6                (-35.3712702, 173.7405337)         -35.3712702          173.7405337
7    7                (-45.7255555, 168.2936808)         -45.7255555          168.2936808
8    8                (-40.3284102, 175.8190684)         -40.3284102          175.8190684
9    9                (-45.1299859, 169.5248818)         -45.1299859          169.5248818
10  10         (-37.9503756, 176.93828736155422)         -37.9503756   176.93828736155422
2
Jonathan Leon 3 Juni 2021, 00:27

Data

          Position
0   (45.349586099999996,-75.81031967988278)
1          (-37.77922725,175.2010323246593)
2                 (-42.9945669,170.7100413)
3                  (-39.2711067,174.154795)
4                    (51.2800275,1.0802533)
5         (-41.30222105,172.89453190955697)
6                 (-35.3712702,173.7405337)
7                 (-45.7255555,168.2936808)
8                 (-40.3284102,175.8190684)
9                 (-45.1299859,169.5248818)
10         (-37.9503756,176.93828736155422)

Larutan

 #Strip of the brackets if column is string and not tuple.
 #str.split column to make it a list
 #stack it to dataframe it

 pd.DataFrame(np.vstack(df['Position'].str.strip('\(\)').str.split(',')), columns=['Lat','Long'])




             Lat                Long
0   45.349586099999996  -75.81031967988278
1         -37.77922725   175.2010323246593
2          -42.9945669         170.7100413
3          -39.2711067          174.154795
4           51.2800275           1.0802533
5         -41.30222105  172.89453190955697
6          -35.3712702         173.7405337
7          -45.7255555         168.2936808
8          -40.3284102         175.8190684
9          -45.1299859         169.5248818
10         -37.9503756  176.93828736155422
1
wwnde 3 Juni 2021, 00:25