繁体   English   中英

使用空字符串或A列中的值(取决于B列中的值)在pandas数据框中添加新列

[英]Add new column in pandas dataframe using empty string or the value from column A depending on the value on column B

我有以下熊猫数据框:

df['price_if_0005'] = df['price'] % Decimal('0.0005')

print(tabulate(df, headers='keys', tablefmt='psql'))

+-----+---------+-------------+-----------------+-----------------+
|     |   price |   tpo_count | tpo             |   price_if_0005 |
|-----+---------+-------------+-----------------+-----------------|
|   0 |  1.4334 |           1 | n               |          0.0004 |
|   1 |  1.4335 |           1 | n               |          0      |
|   2 |  1.4336 |           1 | n               |          0.0001 |
|   3 |  1.4337 |           1 | n               |          0.0002 |
|   4 |  1.4338 |           1 | n               |          0.0003 |
|   5 |  1.4339 |           1 | n               |          0.0004 |
|   6 |  1.434  |           1 | n               |          0      |
|   7 |  1.4341 |           1 | n               |          0.0001 |
|   8 |  1.4342 |           3 | noq             |          0.0002 |
|   9 |  1.4343 |           3 | noq             |          0.0003 |
|  10 |  1.4344 |           3 | noq             |          0.0004 |

我想要另一列为空字符串,或者当'price_if_0005'为0时,来自'price'列的值。IE这将是所需的结果表:

+-----+---------+-------------+-----------------+-----------------+--------+
|     |   price |   tpo_count | tpo             |   price_if_0005 | label  |
|-----+---------+-------------+-----------------+-----------------|--------+
|   0 |  1.4334 |           1 | n               |          0.0004 |        |
|   1 |  1.4335 |           1 | n               |          0      | 1.4335 |
|   2 |  1.4336 |           1 | n               |          0.0001 |        |
|   3 |  1.4337 |           1 | n               |          0.0002 |        |
|   4 |  1.4338 |           1 | n               |          0.0003 |        |
|   5 |  1.4339 |           1 | n               |          0.0004 |        |
|   6 |  1.4340 |           1 | n               |          0      | 1.4340 |
|   7 |  1.4341 |           1 | n               |          0.0001 |        |
|   8 |  1.4342 |           3 | noq             |          0.0002 |        |
|   9 |  1.4343 |           3 | noq             |          0.0003 |        |
|  10 |  1.4344 |           3 | noq             |          0.0004 |        |

我努力了:

df['label'] =  ['' if x == 0 else str(y) for x,y in df['price_if_0005'], df['price']]

但是我得到:

File "<ipython-input-67-90c17f2505bf>", line 3
df['label'] =  ['' if x == 0 else str(y) for x,y in df['price_if_0005'], df['price']]
                                                                       ^
SyntaxError: invalid syntax

只需在熊猫条件下使用.loc即可仅分配所需的行:

df.loc[df['price_if_0005'] == 0, 'label'] = df['price']

完整的例子:

import pandas as pd
from io import StringIO

s = """
         price |   tpo_count | tpo             |   price_if_0005 
   0 |  1.4334 |           1 | n               |          0.0004 
   1 |  1.4335 |           1 | n               |          0      
   2 |  1.4336 |           1 | n               |          0.0001 
   3 |  1.4337 |           1 | n               |          0.0002 
   4 |  1.4338 |           1 | n               |          0.0003 
   5 |  1.4339 |           1 | n               |          0.0004 
   6 |  1.434  |           1 | n               |          0      
   7 |  1.4341 |           1 | n               |          0.0001 
   8 |  1.4342 |           3 | noq             |          0.0002 
   9 |  1.4343 |           3 | noq             |          0.0003 
  10 |  1.4344 |           3 | noq             |          0.0004 """

df = pd.read_csv(StringIO(s), sep="\s+\|\s+")
df.loc[df['price_if_0005'] == 0, 'label'] = df['price']
df['label'].fillna('',inplace=True)
print(df)

输出:

     price  tpo_count  tpo  price_if_0005   label
0   1.4334          1    n         0.0004        
1   1.4335          1    n         0.0000  1.4335
2   1.4336          1    n         0.0001        
3   1.4337          1    n         0.0002        
4   1.4338          1    n         0.0003        
5   1.4339          1    n         0.0004        
6   1.4340          1    n         0.0000   1.434
7   1.4341          1    n         0.0001        
8   1.4342          3  noq         0.0002        
9   1.4343          3  noq         0.0003        
10  1.4344          3  noq         0.0004        

暂无
暂无

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

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