【问题标题】:Python extracting values from sublist withing a columnPython从列中的子列表中提取值
【发布时间】:2021-06-10 11:59:36
【问题描述】:

如果有人能帮我解决这个问题,我将不胜感激。

我在 pandas 数据框中有一列包含数据类型对象。它里面有多个列表。我想要做的是根据经度和纬度挑选坐标并将它们存储在单独的列中。我尝试使用 df["A"]= df['geometry'].str[37:] 创建一个仅包含坐标值的新列。我不确定剩下的值是一组多个列表还是什么。

geometry
0   {"type": "Polygon", "coordinates": [[[-79.41469317817781, 43.6739104164259], [-79.41484930122832, 43.6743388247927], [-79.4155279126094, 43.67606998537741],..]]]}

结果应该是

Longitudes                   Latitudes 

-79.41469317817781          43.6739104164259
-79.41484930122832          43.6743388247927

任何建议将不胜感激

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    试试这个。先将df另存为文本文件

    with open('df.txt', 'r') as data:
      plaintext = data.read()
    
    plaintext = plaintext.replace('-', '--')
    
    #s = plaintext
    
    plaintext = plaintext.replace("-", " ", 1)
    #print(plaintext)
    import re
    
    start = '-'
    end = '--'
    
    a=(plaintext [plaintext .find(start)+len(start):plaintext .rfind(end)])
    a = a.replace("[", "")
    a = a.replace("]", "")
    a = a.replace(",", "")
    a = a.replace("--", "-")
    print(a)
    

    输出

    79.41469317817781 43.6739104164259 -79.41484930122832 43.6743388247927 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多