Joh*_*ohn 0 python dataframe pandas
我有缺失值的数据框:
import pandas as pd
data = {'Brand':['residential','unclassified','tertiary','residential','unclassified','primary','residential'],
'Price': [22000,25000,27000,"NA","NA",10000,"NA"]
}
df = pd.DataFrame(data, columns = ['Brand', 'Price'])
print (df)
Run Code Online (Sandbox Code Playgroud)
导致此数据框:
Brand Price
0 residential 22000
1 unclassified 25000
2 tertiary 27000
3 residential NA
4 unclassified NA
5 primary 10000
6 residential NA
Run Code Online (Sandbox Code Playgroud)
我想用固定值(住宅=1000,未分类=2000)在价格列中填写住宅和未分类的缺失值,但是我不想丢失住宅或未分类价格列中已经存在的任何值,所以输出应该是这样的:
Brand Price
0 residential 22000
1 unclassified 25000
2 tertiary 27000
3 residential 1000
4 unclassified 2000
5 primary 10000
6 residential 1000
Run Code Online (Sandbox Code Playgroud)
完成这项工作的最简单方法是什么
我们可以map用fillna, PS: 你需要确保在你的 df, NA 是 NaN
df.Price.fillna(df.Brand.map({'residential':1000,'unclassified':2000}),inplace=True)
df
Brand Price
0 residential 22000.0
1 unclassified 25000.0
2 tertiary 27000.0
3 residential 1000.0
4 unclassified 2000.0
5 primary 10000.0
6 residential 1000.0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42 次 |
| 最近记录: |