replace/fill 某些特定行的值

replace/fill na values for some specific rows

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

pandas 中是否有类似 SQL (IN) 的语法? 其中地址 IN(Duboce 公园、双子峰、伯纳尔高地、Holly Courts/Ocean 海滩)和 replacing/fillna 用于 'San Francisco' 和 'New York'

谢谢

Pandas df.fillna() 应该可以解决问题。阅读文档:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html

首先使用所需条件过滤您的数据框,然后填充空值:

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 中的竖线 | 运算符类似于 SQL 中的 OR 分隔符。

因此,对于上面的 address,NULL 被替换为 San Francisco。 对其他地址执行相同操作,并将 NULL 替换为 New York.

如果有帮助请告诉我。

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