小编Rou*_*und的帖子

如何为DataFrame的每一行分配组名?

这是一个样本df,

df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
                      'foo', 'bar', 'foo', 'foo'],
                   'B' : ['one', 'one', 'two', 'three',
                      'two', 'two', 'one', 'three'],
                   'C' : np.random.randn(8),
                   'D' : np.random.randn(8)
                  })

     A   B        C           D
0   foo one    1.215172   -0.861875
1   bar one   -0.318147    0.384239
2   foo two    1.729844   -0.173781
3   bar three  0.331456    0.914334
4   foo two    2.038198   -0.354784
5   bar two    0.258204   -1.476305
6   foo one   -0.338992    0.856365
7   foo three -0.311692.  -0.159846
Run Code Online (Sandbox Code Playgroud)

并按A和B分组

for name, group in df.groupby(['A', …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

2
推荐指数
1
解决办法
229
查看次数

标签 统计

dataframe ×1

pandas ×1

python ×1