[英]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?我怎样才能只找到带有NaN
的Temp_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.