Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.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 通过替换不同列中的NAN来合并dataframe内的行_Python_Pandas - Fatal编程技术网

Python 通过替换不同列中的NAN来合并dataframe内的行

Python 通过替换不同列中的NAN来合并dataframe内的行,python,pandas,Python,Pandas,我有一个df: df = pd.DataFrame([[1, np.nan, "filled", 3], [1, "filled", np.nan, 3], [1, "filled", np.nan, 4]], columns = ["a", "b", "c", "d"]) a b c d 0 1 NaN filled 3 1 1 filled NaN 3 2 1 filled NaN 4 我的最终结果应该是: df = pd.DataFram

我有一个df:

df = pd.DataFrame([[1, np.nan, "filled", 3], [1, "filled", np.nan, 3], [1, "filled", np.nan, 4]], columns = ["a", "b", "c", "d"])
    a   b   c   d
0   1   NaN filled  3
1   1   filled  NaN 3
2   1   filled  NaN 4
我的最终结果应该是:

df = pd.DataFrame([[1, "filled", "filled", 3], [1, "filled", np.nan, 4]], columns = ["a", "b", "c", "d"])
    a   b   c   d
0   1   filled  filled  3
1   1   filled  NaN 4
所以我想合并除了b列和c列之外所有方面都相同的行。问题在于,除了b列和c列之外,并非总是会有另一行相同


无法思考如何使用
df.groupby([“a”,“d”])。apply()
来获得我想要的

您可以使用
groupby
+
first
进行检查,它将选择第一个not
NaN
值作为输出

df.groupby(['a','d'],as_index=False).first()
Out[897]: 
   a  d       b       c
0  1  3  filled  filled
1  1  4  filled     NaN