我有一个城市名称列表和一个带有城市、州和zipcode列的df。一些zipcodes丢失了。当zipcode丢失时,我想使用基于城市的通用zipcode。例如,城市是圣何塞,因此zipcode应该是一个通用的“SJïu zipcode”。你知道吗
pattern_city = '|'.join(cities) #works
foundit = ( (df['cty_nm'].str.contains(pattern_city, flags=re.IGNORECASE)) & (df['zip_cd']==0) & (df['st_cd'].str.match('CA') ) ) #works--is this foundit a df?
df['zip_cd'] = foundit.replace( 'SJ_zipcode' ) #nope, error
错误:“pad\u 1d[bool]的数据类型无效”
用where
实现
df['zip_cd'].where( (df['cty_nm'].str.contains(pattern_city, flags=re.IGNORECASE)) & (df['zip_cd']==0) & (df['st_cd'].str.match('CA') ), "SJ_Zipcode", inplace = True) #nope, empty set; all set to nan?
用loc
实现
df['zip_cd'].loc[ (df['cty_nm'].str.contains(pattern_city, flags=re.IGNORECASE)) & (df['zip_cd']==0) & (df['st_cd'].str.match('CA') ) ] = "SJ_Zipcode"
一些可能的解决方案不起作用
df.loc[df['First Season'] > 1990, 'First Season'] = 1
我用它作为df.loc[foundit, 'zip_cd'] = 'SJ_zipcode'
Pandas DataFrame: replace all values in a column, based on condition和Conditional Replace Pandas相似/相同df['c'] = df.apply( lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'], axis=1)
但是,我不是在乘以值https://datascience.stackexchange.com/questions/17769/how-to-fill-missing-value-based-on-other-columns-in-pandas-dataframewhere
的解决方案,但是,它似乎替换了nan不满足条件的值--但是nan值没有帮助https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.where.htmlreplace
的例子,它没有多个条件和模式Replacing few values in a pandas dataframe column with another value一个额外的'want';我想用值更新一个数据帧,我不想创建一个新的数据帧。你知道吗
试试这个:
相关问题 更多 >
编程相关推荐