[英]pandas new column with condition
我认为 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:
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
试试这个你可以根据你的需要调整算法。 这是一个简单的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
这里要求的是如何编写自定义 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)
这个问题在一点上并不一致。 它说
如果值小于 1000,需要用 1 填充值。
但是对于高于 1000 的“Value-1”,预期结果显示Value-2 = 1
: Value-1 = 1104
。
请日后提问时提供工作代码。
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.