我正在尝试更改一个函数,将之前填充的缺失年份作为时间戳(现在的数据类型为int ),改为将缺失的年份视为int,并将其替换为填充as ()。
def nons(row):
for i in row[['name','year_of_release']]:
if row['name']=="LEGO Harry Potter: Years 5-7":
row['year_of_release'].fillna(2012)
else:
continue
return row['year_of_release']
gaming['year_of_release']=gaming.apply(nons,axis=1)然而,它一直把它当作一个时间戳:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10020/3744296997.py in <module>
7 return row['year_of_release']
8
----> 9 gaming['year_of_release']=gaming.apply(nons,axis=1)
~\anaconda3\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, raw, result_type, args, **kwargs)
8738 kwargs=kwargs,
8739 )
-> 8740 return op.apply()
8741
8742 def applymap(
~\anaconda3\lib\site-packages\pandas\core\apply.py in apply(self)
686 return self.apply_raw()
687
--> 688 return self.apply_standard()
689
690 def agg(self):
~\anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
810
811 def apply_standard(self):
--> 812 results, res_index = self.apply_series_generator()
813
814 # wrap results
~\anaconda3\lib\site-packages\pandas\core\apply.py in apply_series_generator(self)
826 for i, v in enumerate(series_gen):
827 # ignore SettingWithCopy here in case the user mutates
--> 828 results[i] = self.f(v)
829 if isinstance(results[i], ABCSeries):
830 # If we have a view on v, we need to make a copy because
~\AppData\Local\Temp/ipykernel_10020/3744296997.py in nons(row)
2 for i in row[['name','year_of_release']]:
3 if row['name']=="LEGO Harry Potter: Years 5-7":
----> 4 row['year_of_release'].fillna(2012)
5 else:
6 continue
AttributeError: 'Timestamp' object has no attribute 'fillna'发布于 2022-07-10 15:48:44
row['year_of_release'].astype(int).fillna(2012) 发布于 2022-07-10 16:13:34
row['year_of_release']是一个时间戳值,它没有任何额外的方法,可以尝试
m1 = gaming['name'].eq('LEGO Harry Potter: Years 5-7')
m2 = gaming['year_of_release'].isna()
gaming.loc[m1 & m2 , 'year_of_release'] = 2012
# or
gaming['year_of_release'] = gaming['year_of_release'].mask(m1 & m2 , 2012)https://stackoverflow.com/questions/72929787
复制相似问题