[英]Rename column names through the loop (Python)
I have a table:我有一张桌子:
I have the table like this:我有这样的表:
import pandas as pd
data = [[20, 15, 10, 5], [20, 15, 10, 5], [20, 15, 10, 5], [20, 15, 10, 5]]
df = pd.DataFrame(data, columns = ['asd', 'bsd', 'tsd', 'pzd'])
df
asd ![]() |
bsd ![]() |
tsd ![]() |
pzd ![]() |
... ![]() |
---|---|---|---|---|
20 ![]() |
15 ![]() |
10 ![]() |
5 ![]() |
... ![]() |
20 ![]() |
15 ![]() |
10 ![]() |
5 ![]() |
... ![]() |
20 ![]() |
15 ![]() |
10 ![]() |
5 ![]() |
... ![]() |
20 ![]() |
15 ![]() |
10 ![]() |
5 ![]() |
... ![]() |
I want to rename all my column names with the pattern like this 'param'+ (index_column +1)
through the loop我想通过循环用像这样的模式重命名我的所有列名
'param'+ (index_column +1)
Desired output:所需的 output:
param1![]() |
param2![]() |
param3![]() |
param4![]() |
... ![]() |
---|---|---|---|---|
20 ![]() |
15 ![]() |
10 ![]() |
5 ![]() |
... ![]() |
20 ![]() |
15 ![]() |
10 ![]() |
5 ![]() |
... ![]() |
20 ![]() |
15 ![]() |
10 ![]() |
5 ![]() |
... ![]() |
20 ![]() |
15 ![]() |
10 ![]() |
5 ![]() |
... ![]() |
Thanks谢谢
No need to use any loop.无需使用任何循环。 Just create a new list of column names, in a list comprehension and set it as new column names:
只需在列表理解中创建一个新的列名列表并将其设置为新的列名:
df.columns = [ 'param' + str(i + 1) for i in range(len(df.columns)) ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.