Skip to content Skip to sidebar Skip to footer

Removing Round Brackets From A Dataframe Of Lat/lon Pairs

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.76

Solution 1:

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
           01049.766795 -7.556440149.766843 -7.556439249.766843 -7.556439>>> df1.info()
<class'pandas.core.frame.DataFrame'>
Int64Index: 3 entries, 0 to 2
Data columns (total 2 columns):
03 non-null float64
13 non-null float64
dtypes: float64(2)
memory usage: 72.0bytes

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
                                 00  (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):
03 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

Post a Comment for "Removing Round Brackets From A Dataframe Of Lat/lon Pairs"