You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pandas pd.concat()报错TypeError:列向合并DataFrame参数异常

解决pandas按列合并DataFrame并重置索引时的TypeError错误

问题场景

需求:按列方式合并pandas DataFrame并重置索引,编写的代码如下:

import pandas as pd
import numpy as np

com_mut = kirc_mut.loc[common_samples]
com_mut = com_mut.sort_index()
com_mut = com_mut.T
com_mut = com_mut.dropna()
com_mut = com_mut.groupby(com_mut.index).first()

com_mut = com_mut.T
l=[com_mut[x].apply(pd.Series).stack() for x in com_mut.columns]
common_mut=pd.concat(l,1).reset_index(level=1,drop=True)

相关数据示例:

  • common_samples[1:10]输出:
Index(['TCGA-A3-3367-01', 'TCGA-A3-3387-01', 'TCGA-B0-4698-01',
       'TCGA-B0-4710-01', 'TCGA-B0-4810-01', 'TCGA-B0-4811-01',
       'TCGA-B0-4815-01', 'TCGA-B0-4818-01', 'TCGA-B0-4821-01'],
      dtype='object')
  • kirc_mut.iloc[0:9,0:9]结构:
pd.DataFrame({'A1BG': {'TCGA-A3-3313-01': 0,
  'TCGA-A3-3313-01A.': 0,
  'TCGA-A3-3317-01': 1,
  'TCGA-A3-3319-01': 0,
  'TCGA-A3-3319-01A.': 0,
  'TCGA-A3-3320-01': 0,
  'TCGA-A3-3320-01A.': 0,
  'TCGA-A3-3331-01': 0,
  'TCGA-A3-3346-01': 0},
 'A1CF': {'TCGA-A3-3313-01': 0,
  'TCGA-A3-3313-01A.': 0,
  'TCGA-A3-3317-01': 0,
  'TCGA-A3-3319-01': 0,
  'TCGA-A3-3319-01A.': 0,
  'TCGA-A3-3320-01': 0,
  'TCGA-A3-3320-01A.': 0,
  'TCGA-A3-3331-01': 0,
  'TCGA-A3-3346-01': 0},
 'A2M': {'TCGA-A3-3313-01': 0,
  'TCGA-A3-3313-01A.': 1,
  'TCGA-A3-3317-01': 0,
  'TCGA-A3-3319-01': 0,
  'TCGA-A3-3319-01A.': 1,
  'TCGA-A3-3320-01': 0,
  'TCGA-A3-3320-01A.': 0,
  'TCGA-A3-3331-01': 0,
  'TCGA-A3-3346-01': 0},
 'A2ML1': {'TCGA-A3-3313-01': 0,
  'TCGA-A3-3313-01A.': 0,
  'TCGA-A3-3317-01': 0,
  'TCGA-A3-3319-01': 0,
  'TCGA-A3-3319-01A.': 0,
  'TCGA-A3-3320-01': 0,
  'TCGA-A3-3320-01A.': 0,
  'TCGA-A3-3331-01': 0,
  'TCGA-A3-3346-01': 0},
 'A4GNT': {'TCGA-A3-3313-01': 0,
  'TCGA-A3-3313-01A.': 0,
  'TCGA-A3-3317-01': 0,
  'TCGA-A3-3319-01': 0,
  'TCGA-A3-3319-01A.': 0,
  'TCGA-A3-3320-01': 1,
  'TCGA-A3-3320-01A.': 0,
  'TCGA-A3-3331-01': 0,
  'TCGA-A3-3346-01': 0},
 'AAAS': {'TCGA-A3-3313-01': 0,
  'TCGA-A3-3313-01A.': 1,
  'TCGA-A3-3317-01': 0,
  'TCGA-A3-3319-01': 0,
  'TCGA-A3-3319-01A.': 0,
  'TCGA-A3-3320-01': 0,
  'TCGA-A3-3320-01A.': 0,
  'TCGA-A3-3331-01': 0,
  'TCGA-A3-3346-01': 0},
 'AADAC': {'TCGA-A3-3313-01': 0,
  'TCGA-A3-3313-01A.': 0,
  'TCGA-A3-3317-01': 0,
  'TCGA-A3-3319-01': 0,
  'TCGA-A3-3319-01A.': 0,
  'TCGA-A3-3320-01': 0,
  'TCGA-A3-3320-01A.': 0,
  'TCGA-A3-3331-01': 0,
  'TCGA-A3-3346-01': 0},
 'AADACL3': {'TCGA-A3-3313-01': 0,
  'TCGA-A3-3313-01A.': 0,
  'TCGA-A3-3317-01': 0,
  'TCGA-A3-3319-01': 0,
  'TCGA-A3-3319-01A.': 0,
  'TCGA-A3-3320-01': 1,
  'TCGA-A3-3320-01A.': 0,
  'TCGA-A3-3331-01': 0,
  'TCGA-A3-3346-01': 0},
 'AADACL4': {'TCGA-A3-3313-01': 0,
  'TCGA-A3-3313-01A.': 0,
  'TCGA-A3-3317-01': 0,
  'TCGA-A3-3319-01': 0,
  'TCGA-A3-3319-01A.': 0,
  'TCGA-A3-3320-01': 1,
  'TCGA-A3-3320-01A.': 0,
  'TCGA-A3-3331-01': 0,
  'TCGA-A3-3346-01': 0}})

报错信息

运行代码后触发TypeError:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [24], in <cell line: 9>()
      7 com_mut = com_mut.T
      8 l=[com_mut[x].apply(pd.Series).stack() for x in com_mut.columns]
----> 9 common_mut=pd.concat(l,1).reset_index(level=1,drop=True)
     10 common_mut.columns=com_mut.columns
     12 common_mut

TypeError: concat() takes 1 positional argument but 2 were given

问题原因

新版本pandas中,pd.concat()仅接受一个必选位置参数(要合并的对象序列),原代码中直接传入的第二个位置参数1用于指定按列合并,现在必须显式使用关键字参数axis=1,否则会被识别为多余的位置参数,触发TypeError。

修正后的代码

将pd.concat(l,1)修改为pd.concat(l, axis=1),完整代码如下:

import pandas as pd
import numpy as np

com_mut = kirc_mut.loc[common_samples]
com_mut = com_mut.sort_index()
com_mut = com_mut.T
com_mut = com_mut.dropna()
com_mut = com_mut.groupby(com_mut.index).first()

com_mut = com_mut.T
l=[com_mut[x].apply(pd.Series).stack() for x in com_mut.columns]
common_mut=pd.concat(l, axis=1).reset_index(level=1,drop=True)
# 若需要还原列名,可保留以下代码
common_mut.columns=com_mut.columns

内容的提问来源于stack exchange,提问作者Anon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 13:14:56