简体   繁体   中英

String operations on only certain column names in pandas dataframe

I have column names in a dataframe

ID c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 ... cN

but I want it to look like

ID c01 c02 c03 c04 c05 c06 c07 c08 c09 c10 c11 c12 ... cN

How can I only change select column names without changing any others?

You can use zfill to get the leading zeroes:

l=[]
for i in range(1,20):
    l.append('c'+str(i).zfill(2))
print(l)

['c01', 'c02', 'c03', 'c04', 'c05', 'c06', 'c07', 'c08', 'c09', 'c10', 'c11', 'c12', 'c13', 'c14', 'c15', 'c16', 'c17', 'c18', 'c19']

And then add 'ID' to this list and then assign it as df.columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM