0
               a1   a2      a3                  v_Zones                  l_season  inlier
58             0  21393.0 -34.0247  ...          7_VII                    163    True
999            0  61028.0 -31.9992  ...          44_VI                    135    True
15             0  20296.0 -30.1956  ...           4_Vn                    226    True
746            0  55236.0 -33.1020  ...           2_VI                    174    True
1000           0  61032.0 -32.1942  ...          20_Vc                    158    True

In the dataframe above, I want to replace the strings in v_Zones column based on following logic:

  • Any string ending in Vn should end with VI instead
  • Any string ending in Vc should end with V instead

I tried using pandas replace command, but not sure how to partially replace a string based on regex. Result should look like this:

              a1   a2      a3                  v_Zones                  l_season  inlier
58             0  21393.0 -34.0247  ...          7_VII                    163    True
999            0  61028.0 -31.9992  ...          44_VI                    135    True
15             0  20296.0 -30.1956  ...           4_VI                    226    True
746            0  55236.0 -33.1020  ...           2_VI                    174    True
1000           0  61032.0 -32.1942  ...          20_V                     158    True
1
  • You say you "tried using pandas replace command", can you clarify by providing a minimal reproducible example with what you've tried and the result? Commented Nov 18, 2020 at 23:02

2 Answers 2

3

It's just a simple replace:

 df['V_Zones'] = df.v_Zones.replace({'Vn$':'VI', 'Vc$':'V'}, regex=True)
Sign up to request clarification or add additional context in comments.

Comments

2

You can use Series.str.replace

df['V_Zones']=df['V_Zones'].str.replace('Vn$','VI').replace('Vc$','V')

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.