Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为数据帧的每一行分配组名?_Python_Pandas_Dataframe - Fatal编程技术网

Python 如何为数据帧的每一行分配组名?

Python 如何为数据帧的每一行分配组名?,python,pandas,dataframe,Python,Pandas,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

这是一个示例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
并按A和B分类

for name, group in df.groupby(['A', 'B']):
    print(name)
    print(group)


('bar', 'one')
     A    B         C         D
1  bar  one -0.318147  0.384239
('bar', 'three')
     A      B         C         D
3  bar  three  0.331456  0.914334
('bar', 'two')
     A    B         C         D
5  bar  two  0.258204 -1.476305
('foo', 'one')
     A    B         C         D
0  foo  one  1.215172 -0.861875
6  foo  one -0.338992  0.856365
('foo', 'three')
     A      B         C         D
7  foo  three -0.311692 -0.159846
('foo', 'two')
     A    B         C         D
2  foo  two  1.729844 -0.173781
4  foo  two  2.038198 -0.354784
我们现在有6个小组

问题:如何添加一个名为G的新列,并让其值是由行所属的组确定的组名(例如G1、G2…G6)

以下是df的最终外观:

     A   B        C           D          G
0   foo one    1.215172   -0.861875      G4
1   bar one   -0.318147    0.384239      G1
2   foo two    1.729844   -0.173781      G6
3   bar three  0.331456    0.914334      G2
4   foo two    2.038198   -0.354784      G6
5   bar two    0.258204   -1.476305      G3
6   foo one   -0.338992    0.856365      G4
7   foo three -0.311692.  -0.159846      G5

感谢您的帮助。

我需要重新设定一个待定的公关基础,而这个问题正是促使我着手解决它的原因:-),这将最终为我们提供一个干净的方式来访问此信息。同时,组码实际上存在于groupby对象中,只是隐藏了一点:

In [97]: df["GN"] = df.groupby(["A","B"]).grouper.group_info[0]

In [98]: df["G"] = "G" + (df["GN"] + 1).astype(str)

In [99]: df
Out[99]: 
     A      B         C         D  GN   G
0  foo    one -1.245506  0.307395   3  G4
1  bar    one  0.072989 -0.402182   0  G1
2  foo    two  0.399269  0.794413   5  G6
3  bar  three  0.475859 -0.685398   1  G2
4  foo    two -0.463065 -0.222632   5  G6
5  bar    two  0.696606 -0.999691   2  G3
6  foo    one -1.211876 -0.368574   3  G4
7  foo  three -0.936385 -1.067160   4  G5

你能发一个到那个公关的链接吗?;)非常感谢。正是我想要的。