Reputation: 300
I'm running what I thought was a simple line in python/pandas to attempt to rename columns after converting a list of lists to a dataframe:
df = pd.DataFrame(motif_lists, index=None).transpose().rename(args.plot_labels)
where these are each of the variables:
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'>
but get the following error when trying to either set columns using rename
:
TypeError: 'list' object is not callable
and the following when rename
is replaced with the function column
TypeError: 'RangeIndex' object is not callable
I also tried a new line:
df.columns = args(plot_labels)
but got the same errors. For additional context, the plot_labels variable is coming from argparse:
optional.add_argument('-l', '--labels', dest='plot_labels', metavar='<PLOT_LABELS>', nargs='+', required=False, type=str)
where the user can specify multiple command-line input variables that will get set as a list, but I do call it in another function as a list with no problems.
Upvotes: 2
Views: 6588
Reputation: 25269
You may pass args.plot_labels
directly to index of df constructor as
df = pd.DataFrame(motif_lists, index=args.plot_labels).T
Or if you want use rename
after you need passing a mapper such as dictionary with 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)
Upvotes: 1