gpt4 book ai didi

python - 类型错误 : 'RangeIndex' object is not callable when trying to assign columns in pandas

转载 作者:行者123 更新时间:2023-12-01 06:50:58 26 4
gpt4 key购买 nike

我正在 python/pandas 中运行我认为是简单的一行,以尝试在将列表列表转换为数据帧后重命名列:

df = pd.DataFrame(motif_lists, index=None).transpose().rename(args.plot_labels)

其中每个变量是:

print(motif_lists)
[['HIVEP1', 'ZNF443', 'ZNF181', 'ZNF770', 'RORA', 'ZNF571', 'BACH1', 'ZNF235', 'ZNF418', 'ZNF782', 'ZNF141', 'ZNF384', 'ZNF287', 'MAFK', 'ZNF496', 'PRDM6', 'HIVEP1', 'ATOH1', 'NR1I2', 'ZNF487', 'ZNF416', 'ZNF774', 'ZNF324B', 'ZNF787', 'RFX3', 'SREBF1', 'ZNF146', 'RFX5', 'STAT6', 'INSM1', 'TEAD3', 'ZNF519', 'MYCL', 'ZNF441', 'ZBTB1'], ['ATOH1', 'ZNF443', 'ZNF181', 'ZNF770', 'NFKB2', 'ZNF571', 'ZNF18', 'NR1I2', 'MAFK', 'ZNF496', 'GRHL2', 'RORA', 'ZNF235', 'ZNF287', 'ZNF774', 'TFAP2B', 'NFYA', 'HES1', 'ZNF146', 'ZNF93', 'PRDM6', 'RELB', 'ZNF708', 'ZNF141', 'ZNF75D', 'ZNF280A'], ['TBX1', 'NR1I2', 'ZNF443', 'ZNF181', 'ZFP42', 'ZNF770', 'ZNF571', 'ZNF287', 'ZNF235', 'BACH1', 'TGIF1', 'ZNF18', 'ZNF496', 'PRDM6', 'CREB3L1', 'ZNF487', 'KLF1', 'GRHL2', 'ZFP69', 'HES1', 'PBX3', 'ZNF384', 'TBX2', 'SREBF1', 'ZNF778', 'MEIS1', 'ZFX', 'ZNF610', 'ZNF610', 'ZNF774', 'MYCL', 'ZNF75D', 'ZNF519', 'ZNF121', 'TFAP2B', 'ZNF614', 'ZNF146']]
print(type(motif_lists))
<class 'list'>
print(args.plot_labels)
['B2B vs LC2', 'A549 vs B2B', 'A549 vs LC2']
print(type(args.plot_labels))
<class 'list'>

但在尝试使用重命名设置列时出现以下错误:

TypeError: 'list' object is not callable

以及当 rename 替换为函数 column 时的以下内容

TypeError: 'RangeIndex' object is not callable

我还尝试了一条新线:

df.columns = args(plot_labels)

但遇到了同样的错误。对于其他上下文,plot_labels 变量来自 argparse:

optional.add_argument('-l', '--labels', dest='plot_labels', metavar='<PLOT_LABELS>', nargs='+', required=False, type=str)

用户可以指定多个命令行输入变量,这些变量将设置为列表,但我确实在另一个函数中将其称为列表,没有任何问题。

最佳答案

您可以将 args.plot_labels 直接传递给 df 构造函数的索引

df = pd.DataFrame(motif_lists, index=args.plot_labels).T

或者,如果您想在需要传递映射器(例如带有 axis=1 的字典)后使用 rename

mapper = dict(zip(range(len(args.plot_labels)), args.plot_labels))
df = pd.DataFrame(motif_lists, index=None).T.rename(mapper, axis=1)

关于python - 类型错误 : 'RangeIndex' object is not callable when trying to assign columns in pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59004059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com