1

I have a simple data frame which might look like this:

| Label   | Average BR_1 | Average BR_2 | Average BR_3 | Average BR_4 |
| ------- | ------------ | ------------ | ------------ | ------------ |
| Label 1 | 50           | 30           | 50           | 50           |
| Label 2 | 60           | 20           | 50           | 50           |
| Label 3 | 65           | 50           | 50           | 50           |

What I would like to be able to do is to add a % symbol in every column.

I know that I can do something like this for every column:

df['Average BR_1'] = df['Average BR_1'].astype(str) + '%'

However, the problem is, that I read in the data from a CSV file which might contain more of these columns, so instead of Average BR_1 to Average BR_4, it might contain Average BR_1 to say Average BR_10.

So I would like this change to happen automatically for every column which contains Average BR_ in its column name.

I have been reading about .loc but I managed only to change column values to an entirely new value like so:

df.loc[:, ['Average BR_1', 'Average BR_2']] = "Hello"

Also, I haven't yet been able to implement regex here.

I tried with a list:

colsArr = [c for c in df.columns if 'Average BR_' in c]
print(colsArr)

But I did not manage to implement this with .loc.

I suppose I could do this using a loop, but I feel like there must be some better pandas solution, but I can not figure it out.

Could you help and point me in the right direction?

Thank you

2 Answers 2

2
# extract the column names that need to be updated
cols = df.columns[df.columns.str.startswith('Average BR')]

# update the columns
df[cols] = df[cols].astype(str).add('%')

print(df)
     Label Average BR_1 Average BR_2 Average BR_3 Average BR_4
0  Label 1          50%          30%          50%          50%
1  Label 2          60%          20%          50%          50%
2  Label 3          65%          50%          50%          50%

working example

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

Comments

2

You can use df.update and df.filter

df.update(df.filter(like='Average BR_').astype('str').add('%'))
df

Out:

   Label     Average BR_1   Average BR_2   Average BR_3   Average BR_4
0  Label 1            50%            30%            50%            50%
1  Label 2            60%            20%            50%            50%
2  Label 3            65%            50%            50%            50%

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.