这是过滤到唯一值的有效方法吗?

2024-09-30 02:26:27 发布

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

我继承了一段遗留代码,不明白为什么这个代码块会抛出断言错误,非常感谢您的帮助

# rename columns and filter out null values
df_cipSP_PP = cipSP_PP_raw.rename(columns={'Asset/Tag Number': 'cipSP UID'})
df_cipSP_PP_select = df_cipSP_PP.loc[df_cipSP_PP['cipSP UID'].notnull()].copy()
df_cipSP_PP_select['cipSP UID'] = df_cipSP_PP_select['cipSP UID'].astype(str)

# take only one unique uid, eliminating nulls
df_cipSP_PP_rolled = df_cipSP_PP_select.groupby(by='cipSP UID').max().reset_index()

# merge farf against cip sharepoint
PP_sources = pd.merge(how='left', left=PP_farf_cipacc_other2, right=df_cipSP_PP_rolled['cipSP UID'].to_frame(),
                      left_on='FARF TAG', right_on='cipSP UID')
PP_sources = PP_sources.rename(columns={'cipSP UID': 'CIP'})

这是我试图解决的错误代码

 assert len(locs) == result.shape[1]

Tags: columns代码rightdfuidonmergeleft
2条回答

这不会过滤到唯一的值。这是按id对项目进行分组,然后只取其中一个(如命令所示)

要筛选唯一值,请查看函数:unique。这会让你走上正轨

df['column_name'].unique()

这将返回所有唯一的值

df['column_name'].unique().tolist()在列表中具有所有唯一值

相关问题 更多 >

    热门问题