我有这个数据框:
import pandas as pd
df = pd.DataFrame({'subject': ['a', 'a', 'b', 'b', 'c', 'd'],
'level': ['hard', None, None, 'easy', None, 'medium']})
print(df)
subject level
0 a hard
1 a None
2 b None
3 b easy
4 c None
5 d medium
使用代码时:
df.groupby('subject').transform(lambda group: print(group))
I got four printed groups. That's ok because we have four subjects : a
, b
, c
and d
But I don't understand the group 2, i feel like transform have accumulated the values of the two first groups. Also, there is a weird indentation that seem to separate the first group from the second one
# ------------------------ group1
0 hard
1 None
Name: level, dtype: object
# ------------------------ group2
level
0 hard
1 None
2 None
3 easy
Name: level, dtype: object
# ------------------------ group3
4 None
Name: level, dtype: object
# ------------------------ group4
5 medium
Name: level, dtype: object
有人能给我解释一下其中的逻辑吗?