1

Hello i have one pandas datafrmae and a list

my data frame

xy_123        ba_322         ab_321      zx_223

  1            1                1           1 

  s2           f32             r32          s223

list= [ "xy_123_8.4", "ba_322_9.5", "ab_321_8.4", "zx_223_9.5"]

output i am looking at

xy_123_8.4        ba_322_9.5        ab_321_8.4     zx_223_9.5

       1            1                1           1 

       s2           f32             r32          s223

I have large dataframe want to change column name is there any posibility do from python pandas

2 Answers 2

1

Use rename by dictionary:

L =  [ "xy_123_8.4", "ba_322_9.5", "ab_321_8.4", "zx_223_9.5"]

d = {x.rsplit('_', 1)[0]:x for x in L}

df = df.rename(columns=d)
print (df)
  xy_123_8.4 ba_322_9.5 ab_321_8.4 zx_223_9.5
0          1          1          1          1
1         s2        f32        r32       s223
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much worked fine , same how can change row names
0

I recommend not to use list as a variable name. in your case

df.columns = list

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.