我想为python熊猫中的每个循环添加一个新值
PRODUCT PRICE
0 ABC 5000
1 ABB 2500
2 ABE 1800
3 DCB 7200
如果df1如上,
for i in df1['PRICE']:
if i>3000:
PURCHASE = True
else:
PURCHESE = False
循环后,我想像下面这样制作df1
PRODUCT PRICE PURCHSE
0 ABC 5000 True
1 ABB 2500 False
2 ABE 1800 False
3 DCB 7200 True
我该怎么做?
答案 0 :(得分:1)
Pandas的语言与您在普通对话中使用的语言有些相似:购买是指PRICE> 3000。
df1['PURCHASE'] = df1['PRICE'] > 3000
答案 1 :(得分:0)
能否请您在此处使用<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<body>
<div id="listProducts" class="listagem borda-alpha ">
<ul data-produtos-linha="3">
<li class="listagem-linha ">
<ul class="">
<li class="span4">
<div class="listagem-item">
<a href="https://test.com/2-test" class="produto-sobrepor" title="TEST 2">2TH ELEMENT</a>
<div class="another 1">1. another div 1</div>
<div class="another 2">1. another div 2</div>
</div>
</li>
<li class="span4">
<div class="listagem-item">
<a href="https://test.com/3-test" class="produto-sobrepor" title="TEST 3">3RD ELEMENT</a>
<div class="another 1">2. another div 1</div>
<div class="another 2">2. another div 2</div>
</div>
</li>
<li class="span4">
<div class="listagem-item">
<a href="https://test.com/1-test" class="produto-sobrepor" title="TEST 1">1st ELEMENT</a>
<div class="another 1">3. another div 1</div>
<div class="another 2">3. another div 2</div>
</div>
</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
进行尝试。
np.where
当我们使用名为import pandas as pd
import numpy as np
df['PURCHASE']=np.where(df2.PRICE>3000,'TRUE','FALSE')
的新列打印新df
的值时,其值如下。
PURCHASE