Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.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,我想基于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

我想基于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: 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