0

I want to go though a column in a pd dataframe and replace all the corresponding values with the appropriate values.

values range from 1-46 in column A.

x=0
y=1000


1=0
2=1
3-22=x+50
23-46=y+100


Input:
Column A

1
2
3
4
23
24


Expected Output:
0
1
50
100
1100
1200
2
  • 1
    why 24 became 1200 ? Commented Aug 14, 2019 at 1:10
  • Each iteration between 23-46 adds 100 starting at 1000. so 23=1100, 24=1200,25=1300, etc. Commented Aug 14, 2019 at 1:54

1 Answer 1

2

You can use pandas.Series.map with a lambda function that maps each old value to its new one (using formulas I guessed from your example):

...

x=0
y=1000

df['A'] = df['A'].map(lambda n:
  n - 1 if n <= 2 else (
    x + 50 * (n - 2) if 3 <= n <= 22 else (
      y + 100 * (n - 22)
    )
  )
)

print(df)

Output:

      A
0     0
1     1
2    50
3   100
4  1100
5  1200
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.