Pandas:合并具有相似名称的列

4
Politics  Politics  Politics    Arts/Culture  Arts/Culture  Arts/Culture
  nan       nan        c         nan            nan          c
  nan        b         nan        a             nan          nan
  nan        b         nan        a             nan          nan
  a          nan       nan        nan           c            nan   

基本上,这个操作将在整个数据框中进行。 我想将相似的列合并到下面的数据框中。

Politics    Arts/Culture  
 c              c
 b              a
 b              a
 a              c


1
列名实际上是相似的还是相同的?我知道默认情况下,pandas会在导入时重命名这些列,除非您手动声明或通过某些函数声明了列。 - Umar.H
它们是相同的。 - Olutomilayo Dolapo
2个回答

4

3
尝试使用 level=0axis=1groupby,然后使用 first
s=df.groupby(level=0,axis=1).first()
  Arts/Culture Politics
0            c        c
1            a        b
2            a        b
3            c        a

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接