df:
address city
BlockOf13thSt Treasure Isla
Lincoln Presidio
Duboce Park Unknown
Twin Peaks Unknown
Bernal Heights NaN
Holly Courts Unknown
Ocean Beach NaN
Maiden Ln NaN
Avenue N NaN
输出
address city
BlockOf13thSt Treasure Isla
Lincoln Presidio
Duboce Park San Francisco
Twin Peaks San Francisco
Bernal Heights San Francisco
Holly Courts San Francisco
Ocean Beach San Francisco
Maiden Ln New York
Avenue N New York
大熊猫中是否有任何语法,例如SQL(IN)? 地址为IN(Duboce Park,Twin Peaks,Bernal Heights,Holly Courts / Ocean Beach)和“旧金山”和“纽约”的替换/ fillna
谢谢
答案 0 :(得分:0)
熊猫df.fillna()应该可以解决问题。阅读文档:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html
答案 1 :(得分:0)
首先使用所需条件过滤数据框,然后填充空值:
df[(df.address == 'Duboce Park') | (df.address == 'Twin Peaks') | (df.address =='Bernal Heights') | (df.address == 'Holly Courts') | (df.address == 'Ocean Beach')].fillna('San Francisco')
pandas中的管道|
运算符类似于OR
中的SQL
分隔符。
因此,对于以上address
,将NULL替换为San Francisco
。
这样做,其他地址也一样,并用New York
替换NULL。
让我知道这是否有帮助。
答案 2 :(得分:0)
import pandas as pd
#Replace all those localities with 'San Francisco'. For this we use .isin() function
df.loc[df['address'].isin(pd.Series(['Duboce Park','Twin Peaks','Bernal Heights','Holly Courts','Ocean Beach'])),'city']='San Francisco'
#Replace all NaNs with 'New York' with fillna().
df = df.fillna('New York')
df
Out[47]:
address city
0 BlockOf13thSt Treasure Isla
1 Lincoln Presidio
2 Duboce Park San Francisco
3 Twin Peaks San Francisco
4 Bernal Heights San Francisco
5 Holly Courts San Francisco
6 Ocean Beach San Francisco
7 Maiden Ln New York
8 Avenue N New York