简体   繁体   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

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

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: 只需在熊猫条件下使用.loc即可仅分配所需的行:

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        

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

相关问题 Pandas:创建新列并根据字符串列中的值(子字符串)和另一列上的值添加值 - Pandas: Create new column and add value depending on value (substring) in a string column and value on another column 在 dataframe 中查找值并在 pandas 的新列中添加先例列值 - find a value in a dataframe and add precedent column value in a new column in pandas Pandas:添加新列并按条件从另一个dataframe赋值 - Pandas: Add new column and assigning value from another dataframe by condition 将新列添加到 Pandas dataframe,其值来自 function - Add a new column to a Pandas dataframe with a value from a function 根据 A 列的值对 B 列的值应用操作 - Pandas - Apply an operation on the value of column B depending on the value of column A - Pandas 如何根据索引将某个值分配到 Pandas 数据框中的新列中 - How to assign a certain value into a new column in a pandas dataframe depending on index 如何为数据框的新列添加字符串作为值 - How to add string as value for new column for dataframe 使用column及其值在pandas数据框中创建一个新列 - Create a new column in pandas dataframe using column and its value 根据值是否为 null 创建 pandas dataframe 列 - Create a pandas dataframe column depending if a value is null or not 根据 pandas 中另一个值向新列添加值 - Add value to new column depending on values in another in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM