简体   繁体   English

pandas 有条件的新列

[英]pandas new column with condition

I am having a data frame as below:我有一个数据框如下:

数据

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.我需要创建一个新列,名称为 Value-2:如果 value-1 小于 500,则需要用 0.5 填充该值。 if the value is less than 1000, you need to fill the value with 1.如果该值小于1000,则需要用1填充该值。

Expected Result:预期结果:

输出

Can someone help me?有人能帮我吗?

I think np.where function will work efficiently on huge data as well.我认为 np.where function 也能有效处理海量数据。

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: 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

Try this you can adapt the algorithm according to your needs.试试这个你可以根据你的需要调整算法。 Here is a simple if / else .这是一个简单的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使用自定义 function

As requested here is how to write a custom function to have more flexibility than a one-liner lambda function.这里要求的是如何编写自定义 function 比单行 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.如果值小于 1000,需要用 1 填充值。

But the expected result shows a Value-2 = 1 for a "Value-1" higher than 1000: Value-1 = 1104 .但是对于高于 1000 的“Value-1”,预期结果显示Value-2 = 1Value-1 = 1104

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM