我正在编写代码来从数据框的每个列中获取值并对其进行一些处理。只要有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
这里有可能使用与NaN
s一起使用的pandas函数:
df_work['specialties'] = df_work['specialties'].str.lower().str.replace(' ','')
如果需要使用NaN
s测试它由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