【问题标题】:Adding Lat Lon coordinates to separate columns (python/dataframe)将经纬度坐标添加到单独的列(python/dataframe)
【发布时间】:2016-09-08 17:08:18
【问题描述】:

我确信这是一件简单的事情,但我是 Python 新手,无法解决!

我有一个数据框,其中一列包含坐标,我想删除括号并将纬度/经度值添加到单独的列中。

当前数据框:

gridReference
(56.37769816725615, -4.325049868061924) 
(56.37769816725615, -4.325049868061924) 
(51.749167440074324, -4.963575226888083)   

想要的数据框:

Latitude               Longitude
56.37769816725615     -4.325049868061924
56.37769816725615     -4.325049868061924
51.749167440074324    -4.963575226888083 

感谢您的帮助

编辑: 我试过了:

df['lat'], df['lon'] = df.gridReference.str.strip(')').str.strip('(').str.split(', ').values.tolist()

但我得到了错误:

AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

然后我尝试添加:

df['gridReference'] = df['gridReference'].astype('str')

并得到错误:

ValueError: too many values to unpack (expected 2)

任何帮助将不胜感激,因为我不知道如何使这项工作! :)

编辑: 我不断收到错误 AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

df.dtypes 的输出是:

<class 'pandas.core.frame.DataFrame'> Int64Index: 22899 entries, 0 to 22898 Data columns (total 1 columns): LatLon 22899 non-null object dtypes: object(1)

df.info() 的输出是:

gridReference object dtype: object

【问题讨论】:

  • 请提供您的数据样本以及您的尝试。
  • 我添加了一些信息。如果您能以任何方式提供帮助,我们将不胜感激!

标签: python pandas dataframe


【解决方案1】:
df['gridReference'].str.strip('()')                               \
                   .str.split(', ', expand=True)                   \
                   .rename(columns={0:'Latitude', 1:'Longitude'}) 

             Latitude           Longitude
0   56.37769816725615  -4.325049868061924
1   56.37769816725615  -4.325049868061924
2  51.749167440074324  -4.963575226888083

【讨论】:

  • 很好 - 但我认为您可以在第二列中添加条形空格并将输出转换为float(我认为更好的是浮点数,因为字符串看起来像浮点数)
  • 感谢您的建议。我认为astype(float) 不考虑整个小数位(精确到 6 位,尽管您可以通过在开头设置它来更改它)。我已经把它留给 OP 考虑他想如何从这里继续前进。
  • 好的,没问题。
  • 是的,这是另一种解决方案。
  • 无论我做什么,我都会不断收到错误Can only use .str accessor with string values, which use np.object_ dtype in pandas。我需要添加什么才能使其正常工作?感谢您的帮助
【解决方案2】:

回答问题: 在我的代码中,输入是一列与 b 列类似的元组。 需要的输出是两列,类似于我的答案中的 b1 和 b2 列。

创建了一个数据框: 在 [2] 中:df = pd.DataFrame({'a':[1,2], 'b':[(1,2), (3,4)]})

In [3]: df                                                                                                                                                                      
Out[3]: 
   a       b
0  1  (1, 2)
1  2  (3, 4)

将列转换为列表:

In [4]: df['b'].tolist()                                                                                                                                                        
Out[4]: [(1, 2), (3, 4)]

使用列表创建所需的数据框[需要输出]:

In [5]: pd.DataFrame(df['b'].tolist(), index=df.index)                                                                                                                                          
Out[5]: 
   0  1
0  1  2
1  3  4

我们还可以尝试使用以下代码在同一数据帧中获取输出: [6]中:df[['b1', 'b2']] = pd.DataFrame(df['b'].tolist(), index=df.index)

In [7]: df                                                                                                                                                                      
Out[7]: 
   a       b  b1  b2
0  1  (1, 2)   1   2
1  2  (3, 4)   3   4

【讨论】:

  • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请edit您的回答添加解释并说明适用的限制和假设。
  • 当然@SurajRao 我会根据您的建议更新和编辑答案
【解决方案3】:
>>> df = pd.DataFrame({'latlong': ['(12, 32)', '(43, 54)']})
>>> df
    latlong
0  (12, 32)
1  (43, 54)

>>> split_data = df.latlong.str.strip(')').str.strip('(').str.split(', ')
>>> df['lat'] = split_data.apply(lambda x: x[0])
>>> df['long'] = split_data.apply(lambda x: x[1])
    latlong lat long
0  (12, 32)  12   43
1  (43, 54)  32   54

【讨论】:

  • 我收到错误消息:“AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas” - 我需要做什么才能让它工作? - 谢谢。
  • df.dtypes 显示:gridReference object dtype: object
  • 编辑了答案。还要检查您的数据中是否存在任何 NA
  • 感谢您的帮助,但它仍然给出相同的错误 - 我认为没有任何 NAs
猜你喜欢
  • 2015-04-27
  • 2013-02-14
  • 2015-10-28
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
相关资源
最近更新 更多