从具有 pandas 中相应值的所有行中形成单行

Form single Row from all rows with corresponding values in pandas

我的数据框如下:

    2017            2018
    A     B    C    A    B    C
0   12    NaN  NaN  98   NaN  NaN 
1   NaN   23   NaN  NaN  65   NaN
2   NaN   NaN  45   NaN  NaN  43

我想将这个数据框转换成:

    2017            2018
    A     B    C    A    B    C
0   12    23   45   98   65  43 

首先填充缺失值,然后select第一行双[]一行DataFrame:

df = df.bfill().iloc[[0]]
#alternative
#df = df.ffill().iloc[-1]]
print (df)
   2017              2018            
      A     B     C     A     B     C
0  12.0  23.0  45.0  98.0  65.0  43.0

可以按列求和:

import pandas as pd
import numpy as np

# Create DataFrame:
tmp = np.hstack((np.diag([12., 23., 42.]), np.diag([98., 65., 43.])))
tmp[tmp == 0] = np.NaN
df = pd.DataFrame(tmp, )

# Sum:
df2 = pd.DataFrame(df.sum(axis=0)).T

导致:

      0     1     2     3     4     5
0  12.0  23.0  42.0  98.0  65.0  43.0

这很方便,因为默认情况下 Dataframe.sum 会忽略 NaN。一些注意事项:

  • 在这种方法中会丢失列名。
  • All-NaN 列将在结果中 return 0