JavaScript
x
33
33
1
import pandas as pd
2
3
dict1 = {
4
"brand": "Ford",
5
"model": "Mustang",
6
"year": 1964
7
}
8
9
dict2 = {
10
"brand": "Ford",
11
"model": "F150",
12
"year": 1999
13
}
14
15
dict3 = {
16
"brand": "Chevy",
17
"model": "Malibu",
18
"year": 1972
19
}
20
21
d = {
22
"col0": ["GM", "GM", "Dodge"],
23
"col1": [dict1, dict3, dict2],
24
"col2": [dict3, dict2, dict2],
25
"col3": [dict1, dict2, dict3]
26
}
27
28
df = pd.DataFrame(d)
29
30
grouped = df.groupby(['col0'], as_index=False)
31
first = lambda a : a[0]
32
df = grouped.agg({'col1':first,'col2':first, 'col3':first})
33
When I try to use the agg function, I’m getting raise KeyError(key) from err
.
What I”m trying to do is combine these based on the columns I’m grouping by and I want to take the first dict after grouping.
I want the output to be what you see below and I don’t really care which “GM” is kept. I arbitrarily chose the first, which is fine.
JavaScript
1
7
1
d = {
2
"col0": ["GM", "Dodge"],
3
"col1": [dict1, dict2],
4
"col2": [dict3, dict2],
5
"col3": [dict1, dict3]
6
}
7
Advertisement
Answer
Use .iloc
:
JavaScript
1
4
1
grouped = df.groupby('col0')
2
first = lambda a : a.iloc[0]
3
df = grouped.agg({'col1':first, 'col2': first, 'col3': first})
4