简体   繁体   中英

Pandas - convert header name in tuple format to a string

After computing percentiles within group, the header names is in tuple format like [('A', 0.5), ('A',0.9)...('Z',0.9)] .

The desired output should be:

['P50 A', 'P90 A', ...'P90 Z']

Basically, I want to multiply the decimal by 100 to get percentage and move it up front, append a letter 'P' in every field.

I feel like I should use map or join like suggested here: How to change the columns name from a tuple to string?

But not sure how to deal with the details.

This works also:

original_names = [('A', 0.5), ('A',0.9),('Z',0.9)]
new_names = ['P'+str(int(100*y)) + ' ' + x for x,y in original_names]

Result: ['P50 A', 'P90 A', 'P90 Z']

Current column names:

col_name = [('A', 0.5), ('A',0.9),('Z',0.9)]

Desired column names:

desired_col_named = [f'P{x[1]*100} {x[0]}' for x in col_name]

Assuming you have python 3.6 supporting f strings.

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