这是更新的df:
df = pd.DataFrame({'user_index': [3590,63, 15], 'movie_index': [1514,563, 9],
'genre_index':['10|12|17|35', '4|2|1|8', None], 'cast_index':['46|534', None, '9|27']})
为了获得想要的数据框,在其中向每个元素添加一个值,我尝试:
offset_dct = {'user_index': 2, 'genre_index': 5}
df.astype(str).fillna('').apply(lambda x: [
[int(z) + offset_dct.get(x.name, 0) for z in y.split('|') if z is not None else []] for y in x])
但是它返回了一个错误:
ValueError: ("invalid literal for int() with base 10: 'nan'", 'occurred at index genre_index')
如果没有“NaN”,则此代码将完美运行:
offset_dct = {'user_index': 2, 'genre_index': 5}
df = df.fillna('').astype(str).apply(lambda x: [
[int(z) + offset_dct.get(x.name, 0) for z in y.split('|')] for y in x])
问题在于构建一个列表理解来忽略 NaN 我认为..
任何帮助解决将不胜感激!
相关分类