【发布时间】:2020-03-13 11:41:46
【问题描述】:
我有一个原始列 'All' 的数据框,我将其拆分为 RegionName1 和 RegioName2 列。有重复的条目,例如,德卢斯和德卢斯(明尼苏达大学德卢斯大学。 我想将德卢斯(明尼苏达大学德卢斯大学)之类的字符串转换为 NaN 值。 所以我试过了
unitown['RegionName2'] = [np.nan if '(' in x else x for x in unitown['RegionName2']]
并得到一个错误 TypeError:'float' 类型的参数不可迭代。我还能尝试什么?
unitown=pd.read_table('university_towns.txt', header=None).rename(columns={0:'All'})
unitown['State']=unitown['All'].apply(lambda x: x.split('[edi')[0].strip() if x.count('[edi') else np.NaN).fillna(method="ffill") #.fillna(method="ffill")
unitown['RegionName1'] = unitown['All'].apply(lambda x: x.split('(')[0].strip() if x.count('(') else np.NaN)
unitown['RegionName2'] = unitown['All'].apply(lambda x: x.split(',')[0].strip() if x.count(',') else np.NaN)
unitown['RegionName2'] = [np.nan if '(' in x else x for x in unitown['RegionName2']]
return unitown[unitown.State=='Minnesota']
【问题讨论】:
标签: string pandas dataframe lambda list-comprehension