Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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 - Fatal编程技术网

基于行标签合并行-python

基于行标签合并行-python,python,pandas,Python,Pandas,我有一个df,看起来像这样: q11__business_booking_channels q11__leisure_booking_channels Base 375.012329 NaN Base NaN 374.987669 q11__business_booking_ch

我有一个df,看起来像这样:

      q11__business_booking_channels  q11__leisure_booking_channels
Base                      375.012329                            NaN
Base                             NaN                     374.987669
      q11__business_booking_channels  q11__leisure_booking_channels
Base                      375.012329                     374.987669
我想合并行,使df看起来像这样:

      q11__business_booking_channels  q11__leisure_booking_channels
Base                      375.012329                            NaN
Base                             NaN                     374.987669
      q11__business_booking_channels  q11__leisure_booking_channels
Base                      375.012329                     374.987669
我失败的尝试:

df = df.groupby(df.index)

有什么想法吗?

我想这就是你想要的:

df.groupby(df.index).first()

      q11__business_booking_channels  q11__leisure_booking_channels
Base                      375.012329                     374.987669

两行合并为一行,但值为“375.012329”、“0.0”。我猜它没有合并这些值?没有,但是你的数据与你发布的数据不同,看起来你的第一个非空值是零,第一个函数的目的是它接受第一个非空值,对吧,有意义!你是对的,我将0替换为np.nan,然后它工作了。