I have a pandas dataframe that looks like this:
Area1 Area2
1 2
1 4
1 5
1 9
2 8
2 16
2 4
2 1
3 8
3 9
How can I convert 'Area2' column so that it becomes a list of values for each 'Area1' column
So the output I would want is:
Area1 Area2
1 2, 4, 5, 9
2 8, 16, 4, 1
3 8, 9
I have done this in R previously:
df %>% group_by(Area1) %>% summarise(Area2= toString(sort(unique(Area2))))
I have been trying out groupby() and agg() but have had no success.
Could someone explain what I can use once I have grouped the data using df.groupby('Area1')
Many thanks in advance for any suggestions.
You can groupby and apply list
import pandas as pd
df=pd.read_csv("test.csv")
df.groupby('Area1')['Area2'].apply(list)
The R snippet does string concatenation.
The following line keeps the original type of Area2
.
import pandas as pd
df.groupby('Area1').Area2.apply(pd.Series.tolist).reset_index()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.