我正在运行我认为是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'>
但是尝试使用rename
设置列时出现以下错误:
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)
用户可以在其中指定多个命令行输入变量,这些变量将被设置为列表,但是我确实在另一个函数中将其作为列表调用,没有问题。
答案 0 :(得分:1)
您可以将args.plot_labels
直接传递为df构造函数的索引
df = pd.DataFrame(motif_lists, index=args.plot_labels).T
或者如果您想在使用rename
的映射器(例如字典)之后使用axis=1
mapper = dict(zip(range(len(args.plot_labels)), args.plot_labels))
df = pd.DataFrame(motif_lists, index=None).T.rename(mapper, axis=1)