简体   繁体   English

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

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

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_Rating column with the value from the Farheit column.我需要替换所有的NaN在Temp_Rating从值列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_Rating rows with the NaN s and replace them with the value in the same row of the Farheit column?我怎样才能只找到带有NaNTemp_Rating行,并用Farheit列的同一行中的值替换它们?

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 NaN values with the corresponding value of df.Farheit .首先将任何NaN值替换为df.Farheit的相应值。 Delete the 'Farheit' column.删除'Farheit'列。 Then rename the columns.然后重命名列。 Here's the resulting DataFrame :这是生成的DataFrame

结果数据帧

The above mentioned solutions did not work for me.上述解决方案对我不起作用。 The method I used was:我使用的方法是:

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

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)

The accepted answer uses fillna() which will fill in missing values where the two dataframes share indices.接受的答案使用fillna() ,它将填充两个数据帧共享索引的缺失值。 As explained nicely here , you can use combine_first to fill in missing values, rows and index values for situations where the indices of the two dataframes don't match.正如这里很好地解释的那样,您可以使用combine_first在两个数据帧的索引不匹配的情况下填充缺失值、行和索引值。

df.Col1 = df.Col1.fillna(df.Col2) #fill in missing values if indices match

#or 
df.Col1 = df.Col1.combine_first(df.Col2) #fill in values, rows, and indices

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Pandas 将一列中的 NaN 替换为与列表列相同行的另一列中的值 - Python Pandas replace NaN in one column with value from another column of the same row it has be as list column Python Pandas 用另一列下一行的值替换一列中的 NaN - Python Pandas replace NaN in one column with value from a row below of another column 当列值匹配时,Pandas Dataframe会从行中替换Nan - Pandas Dataframe replace Nan from a row when a column value matches 如何使用 Pandas 根据同一行中另一列的值替换一列中的 NaN 值? - How to replace NaN value in one column based on the value of another column in the same row using Pandas? Python pandas,用同一列上的先前值替换列上的 NAN - Python pandas, replace a NAN on a column with previous value on the same column 在 Python 中使用 Replace() 或 fillna() 将 NAN 替换为 Pandas 中列的字典值 - Replace NAN with Dictionary Value for a column in Pandas using Replace() or fillna() in Python 用熊猫另一列中的值替换一列中的nan:我的代码出了什么问题 - replace nan in one column with the value from another column in pandas: what's wrong with my code 如果特定列的一个值为 NaN,有没有办法使用 ffill 替换整个 pandas dataframe 行? - Is there a way to replace a whole pandas dataframe row using ffill, if one value of a specific column is NaN? Python:将列中的“ NA”替换为另一列的对应行值中的值(反之亦然) - Python: replace “NA” in column with value from corresponding row value of another column (and vice versa) 在 pandas / python 中,三元运算符用来自不同列的值替换字符串列中的 NaN - In pandas / python, ternary operator to replace NaN in string column with value from different column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM