简体   繁体   中英

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

I have the following pandas dataframe:

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 |

I want another column which will be empty string or the value from 'price' column when 'price_if_0005' is 0. IE This would be the desired resulting table:

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

I have tried:

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

But I get:

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

just use .loc with pandas conditions to assign just the rows you need:

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

full example:

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)

Output:

     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        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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