Df以前:
unnamed:0 unnamed:1 unnamed:2
0 Megan 30000 Botany
1 Ann 24000 Psychology
2 John 24000 Police
3 Mary 45000 Genetics
4 Jay 60000 Data Science
对于df,如下所示:
t0 t1 t2
0 Megan 30000 Botany
1 Ann 24000 Psychology
2 John 24000 Police
3 Mary 45000 Genetics
4 Jay 60000 Data Science
我试图重命名未命名的列:
testfile.columns = testfile.columns.str.replace('Unnamed.*', 't')
testfile = testfile.rename(columns=lambda x: x+'x')
发布于 2019-12-13 02:24:26
这将执行从0到您拥有的列数之间的操作。
testfile.columns = ['t{}'.format(i) for i in range(testfile.shape[1])]
发布于 2019-12-13 02:22:04
您可以使用它重置列名并向其添加前缀。
df = df.T.reset_index(drop=True).T.add_prefix('t')
t0 t1 t2
0 Megan 30000 Botany
1 Ann 24000 Psychology
2 John 24000 Police
3 Mary 45000 Genetics
4 Jay 60000 Data Science
发布于 2019-12-13 02:11:49
试试这个:
df.rename(lambda x: x.replace('unnamed:', 't'), axis=1)
产出:
t0 t1 t2
0 Megan 30000 Botany
1 Ann 24000 Psychology
2 John 24000 Police
3 Mary 45000 Genetics
4 Jay 60000 Data Science
https://stackoverflow.com/questions/59315186
复制相似问题