Python Pandas 用第二列对应行的值替换一列中的 NaN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29177498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:10:27  来源:igfitidea点击:

Python Pandas replace NaN in one column with value from corresponding row of second column

pythonpandasdataframenanfillna

提问by edesz

I am working with this Pandas DataFrame in Python.

我正在 Python 中使用这个 Pandas DataFrame。

File    heat    Farheit Temp_Rating
   1    YesQ         75         N/A
   1    NoR         115         N/A
   1    YesA         63         N/A
   1    NoT          83          41
   1    NoY         100          80
   1    YesZ         56          12
   2    YesQ        111         N/A
   2    NoR          60         N/A
   2    YesA         19         N/A
   2    NoT         106          77
   2    NoY          45          21
   2    YesZ         40          54
   3    YesQ         84         N/A
   3    NoR          67         N/A
   3    YesA         94         N/A
   3    NoT          68          39
   3    NoY          63          46
   3    YesZ         34          81

I need to replace all NaNs in the Temp_Ratingcolumn with the value from the Farheitcolumn.

我需要用Temp_Rating列中的值替换列中的所有 NaN Farheit

This is what I need:

这就是我需要的:

File        heat    Temp_Rating
   1        YesQ             75
   1         NoR            115
   1        YesA             63
   1        YesQ             41
   1         NoR             80
   1        YesA             12
   2        YesQ            111
   2         NoR             60
   2        YesA             19
   2         NoT             77
   2         NoY             21
   2        YesZ             54
   3        YesQ             84
   3         NoR             67
   3        YesA             94
   3         NoT             39
   3         NoY             46
   3        YesZ             81

If I do a Boolean selection, I can pick out only one of these columns at a time. The problem is if I then try to join them, I am not able to do this while preserving the correct order.

如果我进行布尔选择,我一次只能选择这些列中的一列。问题是如果我然后尝试加入他们,我无法在保留正确顺序的同时做到这一点。

How can I only find Temp_Ratingrows with the NaNs and replace them with the value in the same row of the Farheitcolumn?

如何只找到Temp_Rating带有NaNs 的行并用Farheit列的同一行中的值替换它们?

采纳答案by Jonathan Eunice

Assuming your DataFrame is in df:

假设您的 DataFrame 位于df

df.Temp_Rating.fillna(df.Farheit, inplace=True)
del df['Farheit']
df.columns = 'File heat Observations'.split()

First replace any NaNvalues with the corresponding value of df.Farheit. Delete the 'Farheit'column. Then rename the columns. Here's the resulting DataFrame:

首先NaN用 的相应值替换任何值df.Farheit。删除该'Farheit'列。然后重命名列。结果DataFrame如下:

resulting DataFrame

结果数据帧

回答by zsad512

The above mentioned solutions did not work for me. The method I used was:

上述解决方案对我不起作用。我使用的方法是:

df.loc[df['foo'].isnull(),'foo'] = df['bar']

回答by felix_as

An other way to solve this problem,

解决这个问题的另一种方法,

import pandas as pd
import numpy as np

ts_df = pd.DataFrame([[1,"YesQ",75,],[1,"NoR",115,],[1,"NoT",63,13],[2,"YesT",43,71]],columns=['File','heat','Farheit','Temp'])


def fx(x):
    if np.isnan(x['Temp']):
        return x['Farheit']
    else:
        return x['Temp']
print(1,ts_df)
ts_df['Temp']=ts_df.apply(lambda x : fx(x),axis=1)

print(2,ts_df)

returns:

返回:

(1,    File  heat  Farheit  Temp                                                                                    
0     1  YesQ       75   NaN                                                                                        
1     1   NoR      115   NaN                                                                                        
2     1   NoT       63  13.0                                                                                        
3     2  YesT       43  71.0)                                                                                       
(2,    File  heat  Farheit   Temp                                                                                   
0     1  YesQ       75   75.0                                                                                       
1     1   NoR      115  115.0
2     1   NoT       63   13.0
3     2  YesT       43   71.0)