python将第一列替换成等差数列,Python Pandas将一列中的NaN替换为第二列的相应行的值...

I am working with this Pandas DataFrame in Python 2.7.

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.

This is what I need:

File heat Observation

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 NaNs and replace them with the value in the same row of the Farheit column?

解决方案

Assuming your DataFrame is in 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. Delete the 'Farheit' column. Then rename the columns. Here's the resulting DataFrame:

6f47f41d997664ba360232b9fa692d01.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值