简体   繁体   English

根据 DataFrame 中的过滤条件将正值转换为负值

[英]Convert positive values based on a filter condition in a DataFrame to negative values

user_id           date         type        transaction_part_sum     transaction_part_count
1000616411022604  2011-12-20   debit                51.58                       1
1000616411022604  2013-06-10   debit                25.52                       1
1000616411022604  2013-11-07   credit               533.29                      1
1000616411022604  2013-12-26   debit                3.82                        1
1000616411022604  2013-12-31   credit               259.68                      1
1000616411022604  2014-01-02   debit                79.10                       1
1000616411022604  2014-02-25   debit                9.99                        1
1000616411022604  2014-03-26   debit                3.42                        1
1000616411022604  2014-04-02   debit                71.90                       1

In a pandas DataFrame as shown above, I want to change the debit row of the "transaction_part_sum" to negative value.在如上所示的 Pandas DataFrame 中,我想将“transaction_part_sum”的借方行更改为负值。

I did this我做了这个

grouped.loc[grouped['type'] == 'debit', 'transaction_part_sum'] = -1 * grouped['transaction_part_sum']

but when printing grouped.但是在打印分组时。 The values in the debit row don't get populated.借方行中的值不会被填充。 If I multiply with any other positive number I get the values populated.如果我乘以任何其他正数,我会得到填充的值。 How can I change the debit row to negative value?如何将借方行更改为负值?

output: 
user_id           date         type         transaction_part_sum      transaction_part_count
1000616411022604  2011-12-20   debit                                            1
1000616411022604  2013-06-10   debit                                            1
1000616411022604  2013-11-07   credit               533.29                      1
1000616411022604  2013-12-26   debit                                            1
1000616411022604  2013-12-31   credit               259.68                      1
1000616411022604  2014-01-02   debit                                            1
1000616411022604  2014-02-25   debit                                            1
1000616411022604  2014-03-26   debit                                            1
1000616411022604  2014-04-02   debit                                            1

Your solution for me working, also should be simplify with mutiple by constant by *= statement:您为我工作的解决方案,也应该通过*=语句通过常量简化为多个:

EDIT: There is dtype for column amount object, it means obviously strings, so first is necessary convert to numeric:编辑:列amount对象有dtype ,这显然是字符串,所以首先需要转换为数字:

dataframe = pd.DataFrame(dataList, columns=['user_id', 'account_id','amount','randString','date','type','string'])
dataframe['amount'] = dataframe['amount'].astype(float)

grouped = dataframe.groupby(['user_id','date','type']).agg({'amount':['sum','count']}).sort_values(by='date')
grouped.loc[grouped['type'] == 'debit', 'transaction_part_sum'] *= -1
print (grouped)
            user_id        date    type  transaction_part_sum  \
0  1000616411022604  2011-12-20   debit                -51.58   
1  1000616411022604  2013-06-10   debit                -25.52   
2  1000616411022604  2013-11-07  credit                533.29   
3  1000616411022604  2013-12-26   debit                 -3.82   
4  1000616411022604  2013-12-31  credit                259.68   
5  1000616411022604  2014-01-02   debit                -79.10   
6  1000616411022604  2014-02-25   debit                 -9.99   
7  1000616411022604  2014-03-26   debit                 -3.42   
8  1000616411022604  2014-04-02   debit                -71.90   

   transaction_part_count  
0                       1  
1                       1  
2                       1  
3                       1  
4                       1  
5                       1  
6                       1  
7                       1  
8                       1  

You can do a simple command to multiply it by -1.您可以执行一个简单的命令将其乘以 -1。

import pandas as pd
df = pd.DataFrame({'trans':[10,20,30,40]})
print (df)
df['trans'] *= -1
print (df)

This will print the results as follows:这将打印如下结果:

   trans
0     10
1     20
2     30
3     40
   trans
0    -10
1    -20
2    -30
3    -40

You can do the same even for a condition.即使对于条件,您也可以这样做。

df.loc[df['trans'] == 20,'trans'] *= -1
print (df)

This will result in -20 but all others say as is.这将导致 -20,但所有其他人都按原样说。

   trans
0     10
1    -20
2     30
3     40

你可以试试 numpy.where

grouped['transaction_part_sum'] = np.where(grouped.type == 'debit', -1 * grouped.transaction_part_sum, grouped.transaction_part_sum)

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

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