Pandas 如何从只留下一个实例的数据帧中删除特定的重复列
我知道上面提到的可能是一个简单的任务,但它已经花了我很多小时没有任何成功。到目前为止,我在网上搜索也没有成功。因此,我有以下数据帧: 并且,我正在寻找以下预期结果: 我使用了以下代码行:Pandas 如何从只留下一个实例的数据帧中删除特定的重复列,pandas,Pandas,我知道上面提到的可能是一个简单的任务,但它已经花了我很多小时没有任何成功。到目前为止,我在网上搜索也没有成功。因此,我有以下数据帧: 并且,我正在寻找以下预期结果: 我使用了以下代码行: # drop repeat columns leaving the first instance only: df_combo = df_combo.drop_duplicates(['Asgn Carr Cd','Fiscal Month'], keep='first') 然而,这并没有达到我想要的结
# drop repeat columns leaving the first instance only:
df_combo = df_combo.drop_duplicates(['Asgn Carr Cd','Fiscal Month'], keep='first')
然而,这并没有达到我想要的结果。提前感谢您的帮助/建议a。请先查看
groupby
df.groupby(level=0,axis=1).first()
可以使用
np.unique
为每个唯一列名获取一个索引,然后切片:
import numpy as np
df.iloc[:, np.unique(df.columns, return_index=True)[1]]
始终建议在您的帖子中以文本格式发布您的示例,而不是在图像/链接/或其他一些网站中发布。这将使试图提供帮助的人的生活变得轻松,请编辑您的帖子,然后让我们知道。