1

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.

I keep getting errors like

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?

If I do .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)

and df.dtypes:

LatLon    object
dtype: object
Mofi
  • 46,139
  • 17
  • 80
  • 143
hsquared
  • 349
  • 2
  • 4
  • 17
  • What do you mean to remove the brackets? You want to print out the content of the list only, without them? Or maybe save to a file? Because in order to hold all these tuples altogether in memory, you'll need brackets or parentheses.. – rafaelc Sep 10 '16 at 02:53
  • I would delete this question, and write a new one, making clear that you are working with `pandas` (tag). Skip the `ipython` tag. And tell what you are trying to do (print, str, write to file, etc). It may even help to show how you created the dataframe, so we can recreate it and demonstrate how to display it. Remember there is a major difference between removing characters from the dataframe itself, and displaying the data without those characters. – hpaulj Sep 10 '16 at 03:30
  • I was going to delete the question but it wouldn't let me. I have added some more details,and yes I want to remove the brackets not just display it with out them. – hsquared Sep 10 '16 at 09:18
  • The problem happened when you created the data frame: You have a data frame of tuples. You can tell it is tuples by the `dtype` being listed as an `object` . Revisit how you are creating the data frame in order to not have tuples and, instead, have a data frame of floats. – dawg Sep 10 '16 at 17:18
  • Please see updated answer. – dawg Sep 10 '16 at 18:31

1 Answers1

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

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
dawg
  • 98,345
  • 23
  • 131
  • 206