1

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.

3
  • 3
    df.columns=df.columns.map('{0[0]} {0[1]}'.format) Commented Oct 16, 2018 at 18:34
  • Can you show how you came up with these in the first place? Looks like you did something "strange" here. Commented Oct 16, 2018 at 18:35
  • df = df[value + grouper].groupby(grouper).quantile(nth_percentile); df = df.unstack(); df = pd.DataFrame(df.to_records()) Commented Oct 17, 2018 at 1:41

2 Answers 2

2

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']

Sign up to request clarification or add additional context in comments.

1 Comment

Or with an f-string: new_names = [f'P{int(100*y)} {x}' for x,y in original_names]
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.