After querying a DB I get a Dataframe like this:
Animal Max Speed
0 Falcon 380.0
1 Falcon 370.0
2 Parrot 24.0
3 Parrot 26.0
As can be seen, Animal column has repeated values and I wanna group that column and get as result (it doesn't matter if the result is in a diff data type, like dict):
Animal Max Speed
0 Falcon 380.0, 370.0
1 Parrot 24.0, 26.0
All the examples I've seen so far for df.groupby(['column_name'])
has another operation (like sum(), count()
) but what I need is to have only one entry along with all the values.
Is there any operation that can be used to perform this change?
Thanks in advance.
d = {"Animal": ["Falcon", "Falcon", "Parrot", "Parrot"], "Speed": [123, 235.2, 323, 223.3]}
df = pd.DataFrame(d)
df["Speed"] = df.Speed.apply(lambda x: str(x))
df['CT_Speed'] = df.groupby(['Animal'])['Speed'].transform(lambda x : ', '.join(x))
df.drop("Speed", axis=1, inplace = True)
df = df.drop_duplicates()
df