Python 基于列值将多个列合并为一个列
我想基于pandas列的值合并pandas列 到 统治-Python 基于列值将多个列合并为一个列,python,pandas,Python,Pandas,我想基于pandas列的值合并pandas列 到 统治- 1.获取所有列中行的第一个非空值。使用“按位置选择第一列”来填充缺少的值: df.bfill(axis=1).iloc[:, 0] 如果None是字符串,请首先将其转换为缺少的值: s = df.replace('None', np.nan).bfill(axis=1).iloc[:, 0] print (s) 0 4 1 4.1 2 4.2 3 NaN 4 4.1 Name: a, dtype: ob
1.获取所有列中行的第一个非空值。使用“按位置选择第一列”来填充缺少的值:
df.bfill(axis=1).iloc[:, 0]
如果None
是字符串,请首先将其转换为缺少的值:
s = df.replace('None', np.nan).bfill(axis=1).iloc[:, 0]
print (s)
0 4
1 4.1
2 4.2
3 NaN
4 4.1
Name: a, dtype: object
如果需要将数值转换为浮动:
s1 = df.replace('None', np.nan).bfill(axis=1).iloc[:, 0].astype(float)
print (s1)
0 4.0
1 4.1
2 4.2
3 NaN
4 4.1
Name: a, dtype: float64
s = df.replace('None', np.nan).bfill(axis=1).iloc[:, 0]
print (s)
0 4
1 4.1
2 4.2
3 NaN
4 4.1
Name: a, dtype: object
s1 = df.replace('None', np.nan).bfill(axis=1).iloc[:, 0].astype(float)
print (s1)
0 4.0
1 4.1
2 4.2
3 NaN
4 4.1
Name: a, dtype: float64