# get length of df's columns
num_cols = len(list(df))
# generate range of ints for suffixes
# with length exactly half that of num_cols;
# if num_cols is even, truncate concatenated list later
# to get to original list length
rng = range(1, (num_cols / 2) + 1)
new_cols = ['Name'] + ['type_' + str(i) for i in rng] + ['expt_' + str(i) for i in rng]
# ensure the length of the new columns list is equal to the length of df's columns
df.columns = new_cols[:num_cols]
编辑:这将更适合于任意数量的列(偶数或奇数)
相关问题 更多 >
编程相关推荐