我试图在特定的assign
后自动integers
column
作为headers
pandas df
到string
。我可以通过读取和计算任何一个columns
中的df
的数量来手动完成此操作。例如
df.columns=['X',1,2,3...]
但我想自动实现这一点,而不必count
columns
的数量。第一个column
应该是string
然后自动将增量integers
分配给其他列。
我尝试使用@ kudeh的建议来实现这一点。
df.columns = 'X' + [i for i in range(1,len(df.columns)+1)]
但是这会返回一个错误:
df.columns = 'X' + [i for i in range(1,len(df.columns)+1)]
TypeError: must be str, not list
预期产出:
df.columns = ['X',1,2,3...]
cols = ['X']
otherCols = [i for i in range(1,len(df.columns))]
cols.extend(otherCols)
df.columns = cols