This should work for you:
df.groupby(['latitude', 'longitude']).aggregate(lambda x: ','.join(map(str, x)))
Answer from HenriChab on Stack OverflowThis should work for you:
df.groupby(['latitude', 'longitude']).aggregate(lambda x: ','.join(map(str, x)))
The object type pandas.core.groupby.generic.DataFrameGroupBy is a list of tuples, where the first element is the groupby element and the second the dataframe for that group.
See the example below:
Creating test dataframe
import pandas as pd
df = pd.DataFrame({"ColA": [1,1,1,2,2,3,3,3], "ColB": [5,5,6,7,7,8,8,9], "ColC": [1,2,3,4,5,6,7,8]})
The test dataframe
>>> df
ColA ColB ColC
0 1 5 1
1 1 5 2
2 1 6 3
3 2 7 4
4 2 7 5
5 3 8 6
6 3 8 7
7 3 9 8
Grouping dataframe
>>> groups = df.groupby(["ColA", "ColB"])
>>> type(groups)
<class 'pandas.core.groupby.generic.DataFrameGroupBy'>
Showing results
>>> for group in groups:
... g, value = group
... print(f"Key = {g}")
... print(value)
... print(80*"-")
...
Key = (1, 5)
ColA ColB ColC
0 1 5 1
1 1 5 2
--------------------------------------------------------------------------------
Key = (1, 6)
ColA ColB ColC
2 1 6 3
--------------------------------------------------------------------------------
Key = (2, 7)
ColA ColB ColC
3 2 7 4
4 2 7 5
--------------------------------------------------------------------------------
Key = (3, 8)
ColA ColB ColC
5 3 8 6
6 3 8 7
--------------------------------------------------------------------------------
IMPORTANT
As commented by @HenriChab, using aggregate or, for example, sum will return a dataframe type not a group type
>>> new_df = df.groupby(["ColA", "ColB"]).sum()
>>> new_df
ColC
ColA ColB
1 5 3
6 3
2 7 9
3 8 13
9 8
Finally you can reset the index.
>>> new_df.reset_index(inplace=True)
>>> new_df
ColA ColB ColC
0 1 5 3
1 1 6 3
2 2 7 9
3 3 8 13
4 3 9 8
Python Pandas <pandas.core.groupby.DataFrameGroupBy object at ...> - Stack Overflow
Return type from `pandas.core.groupby.generic.DataFrameGroupBy.resample`
python - Convert DataFrameGroupBy object to DataFrame pandas - Stack Overflow
How does pandas .groupby() works?
I'm new to pandas and I'd love to know how does the groupby() method work. To clarify I'm familiar with the GROUP BY concept from SQL. What concerns me though is that for some reason this method changes the type of the pandas object to a generator object - <class 'pandas.core.groupby.generic.DataFrameGroupBy'> which isn't printable by itself. When I then use list() on it, it appears to have messed up the columns. What should I do with that?
df_g.apply(lambda x: x)
will return the original dataframe.
The result of kl.aggregate(np.sum) is a normal DataFrame, you just have to assign it to a variable to further use it. With some random data:
>>> df = DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
>>> 'foo', 'bar', 'foo', 'foo'],
... 'B' : ['one', 'one', 'two', 'three',
... 'two', 'two', 'one', 'three'],
... 'C' : randn(8), 'D' : randn(8)})
>>> grouped = df.groupby('A')
>>> grouped
<pandas.core.groupby.DataFrameGroupBy object at 0x04E2F630>
>>> test = grouped.aggregate(np.sum)
>>> test
C D
A
bar -1.852376 2.204224
foo -3.398196 -0.045082