Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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_Multi Index - Fatal编程技术网

Python 将多索引数据帧的行合并到逗号分隔的列表中

Python 将多索引数据帧的行合并到逗号分隔的列表中,python,pandas,dataframe,multi-index,Python,Pandas,Dataframe,Multi Index,给定一个多索引数据帧,我想组合重复的索引对,并将它们的值列为逗号分隔的列表。例如,输入: df = pd.DataFrame({'Last Name' : ['Deere','Deere','Foo' ,'Foo' ,'Man' ], 'First Name': ['John' ,'Jane' ,'Kung' ,'Kung' ,'Karate'], 'Value1': [ 1 , 2

给定一个多索引数据帧,我想组合重复的索引对,并将它们的值列为逗号分隔的列表。例如,输入:

df = pd.DataFrame({'Last Name' : ['Deere','Deere','Foo'   ,'Foo'  ,'Man'   ],
                   'First Name': ['John' ,'Jane' ,'Kung'  ,'Kung' ,'Karate'],
                   'Value1':     [ 1     , 2     , 3      , 4     , 5      ],
                   'Value2':     ['Green','Blue' ,'Yellow','Black','Purple']})

df.set_index(['Last Name','First Name'],inplace=True)
规定:

                          Value1    Value2
Last Name   First Name      
Deere       John          1         Green
            Jane          2         Blue
Foo         Kung          3         Yellow
            Kung          4         Black
Man         Karate        5         Purple
我想将其转换为以下数据帧:

                          Value1    Value2
Last Name   First Name      
Deere       John          1         Green
            Jane          2         Blue
Foo         Kung          3,4       Yellow,Black
Man         Karate        5         Purple

您可以先将列
Value1
转换为
string
by,然后使用
join
按级别
Last Name
first Name

df['Value1'] = df['Value1'].astype(str)
result = df.groupby(level=['Last Name','First Name'], sort=False).agg( ','.join)
print result
                     Value1        Value2
Last Name First Name                     
Deere     John            1         Green
          Jane            2          Blue
Foo       Kung          3,4  Yellow,Black
Man       Karate          5        Purple