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.
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:
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.
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.
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)
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.