any help here?! let's suppose I have a dataframe with two columns:
A | B
1 | b
1 | b
1 | a
2 | a
2 | b
3 | b
3 | c
3 | d
I want to get the first occurrence for each value of the colA it would be something like
A | B
1 | b
2 | a
3 | b
then catch the second occurrence something like that:
A | B
1 | b
2 | b
3 | c
after 3 occurrence
A | B
1 | a
2 | NULL
3 | d
any tips on how to do this??
IIUC, here's one way:
df1 = df.pivot_table(index = 'A', columns = df.groupby('A').cumcount(), values = 'B', aggfunc = sum)
result = [df1[col].reset_index(name='B') for col in df1.columns] #this will give you the list of df's
[ A B
0 1 b
1 2 a
2 3 b,
A B
0 1 b
1 2 b
2 3 c,
A B
0 1 a
1 2 NaN
2 3 d]
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.