将重复索引值合并为单行(单索引)pandas

时间:2018-01-04 02:02:05

标签: python pandas

有一个类似下面的df,并希望将重复的索引值压缩成一行:

            ask     bid
date        
2011-01-03  0.32    0.30
2011-01-03  1.03    1.01
2011-01-03  4.16    4.11

和预期的输出(现在不重要的列名将手动设置):

            ask     bid     ask1    bid1    ask2    bid2
date        
2011-01-03  0.32    0.30    1.03    1.01    4.16    4.11

1 个答案:

答案 0 :(得分:0)

可以执行以下操作来获取您要查找的输出:

       import pandas as pd

       df_1=pd.DataFrame({'date':['2011-01-03','2011-01-03','2011-01-03'],'ask':[0.31,1.05,4.17],'bid':[0.40,1.41,5.11]})

       dfs=list()
       df_count=1
       while df_1['date'].duplicated().any()==True:
         df_count+=1
         b=df_1.drop_duplicates(subset='date',keep='first')
         dfs.append(b)
         df_1=df_1.merge(b,how='outer',on=['date','ask','bid'],indicator=True)
         df_1=df_1[df_1['_merge']=='left_only']
         del df_1['_merge']

      dfs.append(df_1)
      df_final = reduce(lambda left,right: pd.merge(left,right,on='date',suffixes=('_1','_2')), dfs)

输入:

      ask   bid        date
  0  0.31  0.40  2011-01-03
  1  1.05  1.41  2011-01-03
  2  4.17  5.11  2011-01-03

输出:

        ask_1  bid_1     date     ask_2   bid_2   ask   bid

     0   0.31    0.4  2011-01-03   1.05   1.41  4.17  5.11