从经纬度对数据框中删除圆括号 [英] Removing round brackets from a dataframe of lat/lon pairs

查看:99
本文介绍了从经纬度对数据框中删除圆括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定这是一件很简单的事情,但是我似乎遇到了麻烦! (我对此也很陌生.)

I'm sure this is a very simple thing to do but I seem to be having trouble! (I am rather new to this too.)

我有一个包含经度坐标的数据框:

I have a dataframe containing lat long coordinates:

    LatLon
0   (49.766795012580374, -7.556440128791576)
1   (49.766843444728075, -7.556439417755133)
2   (49.766843444728075, -7.556439417755133)

我想删除圆括号/括号,但我无法解决.

I would like to remove the round brackets/parentheses, but I just cannot work it out.

我不断收到类似的错误

AttributeError:只能将.str访问器与字符串值一起使用,后者在熊猫中使用np.object_ dtype

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

但是我不确定该怎么做.

But I'm not sure what to do to fix it.

我认为这是因为类型是对象-所以我需要先将其转换为字符串?

I think it is because the type is object - so I need to convert it to string first?

如果我执行.info():

<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.dtypes:

LatLon    object
dtype: object

推荐答案

关于更新的问题,这是更新的答案.

With the updated question, here is the updated answer.

假设我们有以下元组列表:

Suppose we have this list of tuples:

>>> li
[(49.766795012580374, -7.556440128791576), (49.766843444728075, -7.556439417755133), (49.766843444728075, -7.556439417755133)]

我们可以直接创建一个数据框(基本上是矩阵或列表列表):

We can create a data frame (which, fundamentally is a matrix or a list of lists) directly:

>>> df1=pd.DataFrame(li)
>>> df1
           0         1
0  49.766795 -7.556440
1  49.766843 -7.556439
2  49.766843 -7.556439
>>> df1.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 3 entries, 0 to 2
Data columns (total 2 columns):
0    3 non-null float64
1    3 non-null float64
dtypes: float64(2)
memory usage: 72.0 bytes

请注意,这是一个2列的float数据帧.

Notice this is a 2 column data frame of floats.

但是,想象一下,现在我们有了这个列表,它是元组列表的列表:

However, imagine now we have this list, which is a list of lists of tuples:

>>> li2
[[(49.766795012580374, -7.556440128791576)], [(49.766843444728075, -7.556439417755133)], [(49.766843444728075, -7.556439417755133)]]

如果在此处创建数据框,您将获得示例中的内容:

If you create a data frame here, you get what you have in the example:

>>> df2=pd.DataFrame(li2)
>>> df2
                                 0
0  (49.7667950126, -7.55644012879)
1  (49.7668434447, -7.55643941776)
2  (49.7668434447, -7.55643941776)
>>> df2.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 3 entries, 0 to 2
Data columns (total 1 columns):
0    3 non-null object
dtypes: object(1)

是元组的一列数据帧.

Which is a one column data frame of tuples.

所以我想您的问题在于您最初创建数据框时.除了原始列表或元组列表之外,原始数据还具有元组列表列表(或元组元组列表等)...

So I am guessing your issue is in the initial creation of you data frame. Instead of a list of lists or a list of tuples, your original data has a list of lists of tuples (or a list of tuples of tuples, etc)...

解决方法(如果我是正确的话)是将源列表平整一个级别:

The fix (if I am correct) is to flatten the source list by one level:

>>> pd.DataFrame(t for sl in li2 for t in sl)
           0         1
0  49.766795 -7.556440
1  49.766843 -7.556439
2  49.766843 -7.556439

这篇关于从经纬度对数据框中删除圆括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆