I have a dataframe where I want to group by the ID field and get last letters in GG field. For example, say I have the following:
df1 = pd.DataFrame({
'ID':['Q'] * 3,
'GG':['L3S_0097A','L3S_0097B','L3S_0097C']
})
print (df1)
ID GG
0 Q L3S_0097A
1 Q L3S_0097B
2 Q L3S_0097C
I am trying to groupby ID column and get only last letter in GG column and add it to the defaultdict like this:
{'Q': ['A','B','C']}
Here is the code I tried:
mm = df1.groupby('ID')['GG'].str[-1].apply(list).to_dict()
and also tried the following code:
for i, j in zip(df1.ID,df1.GG):
mm[i].append(j[-1])
but both din't work. May I know how to do it?