Combine two rows in data frame in python
Question:
I have a data frame which has two columns and one column has a duplicate values .I want to combine the rows into one . But I dont want to run any aggregation function . My data frame is
My data frame df
CP CL
0 45538 [757, 1]
1 5548 [712515, 1]
2 7908 [251, 3]
3 7908 [7885, 1]
The result I want is as below
CP CL
0 45538 [757, 1]
1 5548 [712515, 1]
2 7908 [[251, 3],[7885, 1]]
I tried with below code but aggregation doesn’t work
agg_functions = {'CL': 'list'}
df_new = df.groupby(df['CP']).aggregate(agg_functions)
The above code doesn’t work and gives error ,I am not sure how I can combine it using data frame functions.
Answers:
Remove the quotes around your list
agg func since list
is not a builtin agg func in pandas so pandas doesn’t know how to aggregate
Change,
agg_functions = {'CL': 'list'}
to,
agg_functions = {'CL': list}
I have a data frame which has two columns and one column has a duplicate values .I want to combine the rows into one . But I dont want to run any aggregation function . My data frame is
My data frame df
CP CL
0 45538 [757, 1]
1 5548 [712515, 1]
2 7908 [251, 3]
3 7908 [7885, 1]
The result I want is as below
CP CL
0 45538 [757, 1]
1 5548 [712515, 1]
2 7908 [[251, 3],[7885, 1]]
I tried with below code but aggregation doesn’t work
agg_functions = {'CL': 'list'}
df_new = df.groupby(df['CP']).aggregate(agg_functions)
The above code doesn’t work and gives error ,I am not sure how I can combine it using data frame functions.
Remove the quotes around your list
agg func since list
is not a builtin agg func in pandas so pandas doesn’t know how to aggregate
Change,
agg_functions = {'CL': 'list'}
to,
agg_functions = {'CL': list}