是否有任何python函数来检查列表推导中的nan值而不更改它

问题描述 投票:0回答:1

我正在编写代码来从数据框的每个列中获取值并对其进行一些处理。只要有NaN值,我就会遇到异常。我不想用Nan放下列。以前我只是通过捕获异常来解决问题,但现在我无法做到这一点,因为我在这里使用列表推导。有人可以建议一个正确的方法吗?以前我这样解决了:

for index, row in df_work.iterrows():
        descrip = row['description']
        try:
            r = Rake()
            r.extract_keywords_from_text(descrip)
            key_words_dict_scores = r.get_word_degrees()
            row['Key_words'] = list(key_words_dict_scores.keys())
        except Exception as e:
            print(e)
            row['Key_words'] = ''

我想在这里做同样的事情:

df_work['specialties'] = [','.join(x) for x in df_work['specialties'].map(lambda x: x.lower().replace(' ','').split(',')).values]
    df_work['industry'] = [','.join(x) for x in df_work['industry'].map(lambda x: x.lower().replace(' ','').split(',')).values]
    df_work['type'] = [','.join(x) for x in df_work['type'].map(lambda x: x.lower().replace(' ','').split(',')).values]

我在上面的代码中得到了这个错误:

'float' object has no attribute 'lower'

Specialties列包含如下数据:

df_work.loc['TOTAL', 'specialties']

输出>> 'Oil & Gas - Exploration & Production,Upstream,Refining,Trading,Shipping,Marketing,Energy,Crude Oil,Petroleum,Petrochemicals,Liquified Natural Gas,Renewable Energy,Drilling Engineering,Completion & Intervention Engineering,Geology,Geoscientists,IT'

type(df_work.loc['TOTAL', 'specialties'])

输出>> str

运行上面代码后的预期输出应为:OUTPUT >> 'oil&gas-exploration&production,upstream,refining,trading,shipping,marketing,energy,crudeoil,petroleum,petrochemicals,liquifiednaturalgas,renewableenergy,drillingengineering,completion&interventionengineering,geology,geoscientists,it'

type(df_work.loc['TOTAL', 'specialties'])

输出>> str

python-3.x pandas numpy dataframe nan
1个回答
0
投票

这里有可能使用与NaNs一起使用的pandas函数:

df_work['specialties'] = df_work['specialties'].str.lower().str.replace(' ','')

如果需要使用NaNs测试它由isinstance()if-else声明:

df_work['specialties'] = (df_work['specialties']
        .map(lambda x: x.lower().replace(' ','') if isinstance(x, str) else x))

列表理解解决方案:

df_work['specialties'] = [x.lower().replace(' ','') 
                          if isinstance(x, str) 
                          else x 
                          for x in df_work['specialties']]

样品:

df_work = pd.DataFrame({'specialties':['First spec, Sec spec','A Vb,ds RT', np.nan]})
print (df_work)
            specialties
0  First spec, Sec spec
1            A Vb,ds RT
2                   NaN

df_work['specialties'] = [x.lower().replace(' ','') 
                          if isinstance(x, str) 
                          else x 
                          for x in df_work['specialties']]
print (df_work)
         specialties
0  firstspec,secspec
1           avb,dsrt
2                NaN
© www.soinside.com 2019 - 2024. All rights reserved.