-2

I am having a data frame as below:

data

I need to create a new column, with the name Value-2: if the value-1 is less than 500, you need to fill the value with 0.5. if the value is less than 1000, you need to fill the value with 1.

Expected Result:

output

Can someone help me?

Amir Hossein Shahdaei
  • 1,158
  • 1
  • 8
  • 18
Unicorn
  • 43
  • 7
  • What have you already tried and where did you get stuck? And please provide a [MWE](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Marco_CH Dec 31 '22 at 10:06

3 Answers3

2

I think np.where function will work efficiently on huge data as well.

import pandas as pd
import numpy as np

dictionary = {
    "Company" : ['A','A','A','A','A','A'],
    "Value1" : [480,120,876,340,996,1104]
}

dataframe = pd.DataFrame(dictionary)
dataframe["Value2"] = np.where(dataframe["Value1"] < 500, 1, 0.5)

Output:

  Company  Value1  Value2
0       A     480     0.5
1       A     120     0.5
2       A     876     1.0
3       A     340     0.5
4       A     996     1.0
5       A    1104     1.0
1

Try this you can adapt the algorithm according to your needs. Here is a simple if / else.

df['Value-2'] = df['Value-1'].apply(lambda x: 0.5 if x < 500 else 1)

#  Company  Value-1  Value-2
# 0       A      480      0.5
# 1       A      120      0.5
# 2       A      876      1.0
# 3       A      340      0.5
# 4       A      996      1.0
# 5       A     1104      1.0

Using a custom function

As requested here is how to write a custom function to have more flexibility than a one-liner lambda function.

def my_fun(x):
  # can be a switch case or any complex algorithm
  return 0.5 if x < 500 else 1

df['Value-2'] = df['Value-1'].apply(my_fun)

Note

The question is not consistent on one point. It says

if value is less than 1000, need to fill the value with 1.

But the expected result shows a Value-2 = 1 for a "Value-1" higher than 1000: Value-1 = 1104.

Romain
  • 19,910
  • 6
  • 56
  • 65
  • what if i want an extra condition in this code? like if the value-1 is less than 1200, need to replace with 0.8. So how will i change the code? – Unicorn Dec 31 '22 at 08:49
  • @Unicorn Lambda is just a function so you can do whatever you want. So you can simply add a another condition or write a custom function according to your needs. I guess the question was more how to do it (efficiently) in pandas than writing a basic algorithm in Python? – Romain Dec 31 '22 at 08:54
  • I am new to coding. Can you show me how to do that? – Unicorn Dec 31 '22 at 08:56
  • @Unicorn done. You can write the function you want to produce a value from the value of the parameter and then apply it. Hope it answers your question. – Romain Dec 31 '22 at 09:06
0

Please provide a working code in the future when asking questions.

import pandas as pd

Data = {
    "Company" : ['A','A','A','A','A','A'],
    "Value1" : [480,120,876,340,996,1104]
}

DataFrame1 = pd.DataFrame(Data)

DataFrame2 = []

for x in DataFrame1['Value1']:
    if x < 500 : DataFrame2.append(0.5)
    elif x < 1000 or x > 1000 : DataFrame2.append(1) # As the picture given in the question tells Value 2 is 1 when value 1 is 1104
    else : pass

DataFrame1['Value2'] = DataFrame2
print(DataFrame1)
Outputs
  Company  Value1  Value2
0       A     480     0.5
1       A     120     0.5
2       A     876     1.0
3       A     340     0.5
4       A     996     1.0
5       A    1104     1.0

Julia Meshcheryakova
  • 3,162
  • 3
  • 22
  • 42
  • Does not look like the expected result even if the question said row, the expected result shows a new column. – Romain Dec 31 '22 at 08:11
  • @Romain Accidentally pasted the wrong code after making it! Thanks for pointing it out! My solution was to create a new Series and add it to the existing dataframe. – KarthiDiamond97 Dec 31 '22 at 08:31
  • @Unicorn In the result table you have given value 2 is 1 when value 1 is 1104, so I have put append(1) if x is less or more than 1000 – KarthiDiamond97 Dec 31 '22 at 08:47