【问题标题】:Split a pair of strings in bracket using python使用python拆分括号中的一对字符串
【发布时间】:2021-06-02 23:48:39
【问题描述】:

我想将以下数据分成两列,latitudelongitude,并将它们放在一个数据框中。

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)

谁能帮帮我?

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 这似乎是一个简单的正则表达式:\(([-\d.]+), ([-\d.]+)\) 应该这样做。
  • @Mel 我尝试使用 data.str.split(',')

标签: python pandas dataframe split jupyter-notebook


【解决方案1】:

另一种方式:

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】:

    数据

              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)
    

    解决方案

     #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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 2013-03-30
      • 1970-01-01
      相关资源
      最近更新 更多