Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.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 在pandas中的列匹配上替换另一个dataframe中的NaN值的正确方法_Python_Pandas_Numpy - Fatal编程技术网

Python 在pandas中的列匹配上替换另一个dataframe中的NaN值的正确方法

Python 在pandas中的列匹配上替换另一个dataframe中的NaN值的正确方法,python,pandas,numpy,Python,Pandas,Numpy,我是pandas的新手,尝试用列值匹配替换df1中的列值(NaN)和df2。并面临以下错误 df1 unique_col | Measure 944537 NaN 7811403 NaN 8901242114307 1 df2 unique_col | Measure 944537 18 7811403 12 8901242114307 17.5 df1.loc[(df1.unique_c

我是pandas的新手,尝试用列值匹配替换df1中的列值(NaN)和df2。并面临以下错误

df1
unique_col  |  Measure
944537          NaN
7811403         NaN 
8901242114307     1 

df2
unique_col  |  Measure
944537           18
7811403          12 
8901242114307    17.5



df1.loc[(df1.unique_col.isin(df2.unique_col) &
                       df1.Measure.isnull()), ['Measure']] = df2[['Measure']]
我有两个数据帧,有300万条记录,在执行以下操作时遇到以下错误:

ValueError:无法从重复轴重新编制索引


您可以使用函数轻松填充NAN。在您的情况下,如果dfs为(注意索引)

你可以简单地

>>> df.fillna(df2)


    unique_col       Measure
0   944537           18.0
1   7811403          12.0
2   8901242114307    1.0
如果索引与上述不同,则可以将它们设置为相同并使用相同的函数

df = df.set_index('unique_col')
df.fillna(df2.set_index('unique_col'))
df = df.set_index('unique_col')
df.fillna(df2.set_index('unique_col'))