将系列转换为整数,忽略NaN

2024-05-17 19:05:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个表达式,它似乎将我的浮点值转换为整数

df_maa_lu.loc[df_maa_lu["APPROVAL_YEAR"].notnull()]["APPROVAL_YEAR"].apply(lambda x: int(x)) 

返回:

enter image description here

当我尝试重新分配这些值时,它不起作用:

df_maa_lu.loc[df_maa_lu["APPROVAL_YEAR"].notnull()]["APPROVAL_YEAR"] = df_maa_lu.loc[df_maa_lu["APPROVAL_YEAR"].notnull()]["APPROVAL_YEAR"].apply(lambda x: int(x))

返回此警告:

C:\ProgramData\Anaconda3\envs\ariel\lib\site-packages\ipykernel_launcher.py:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy """Entry point for launching an IPython kernel.

我的原始数据帧中仍然有浮动:

enter image description here

我试过这个,但它不喜欢“NaN”值

df_maa_lu.loc[:,  "APPROVAL_YEAR"] = df_maa_lu.loc[:,  "LU_APPROVED_DT"].dt.year.apply(lambda x: int(x))

我要做的就是将非NaN值赋值为int


Tags: thelambdapandasdfvalueyearlocint