'Trying to remove rows with zeros in Pandas, getting KeyError
I am pretty new to Python and Pandas in general, apologies for the noob question.
I am trying to sort out the values that I read from a csv file by removing the items with less than 0.01 in them. Unfortunately, I've been either getting KeyErrors all day, or the error is "TypeError: '>=' not supported between instances of 'str' and 'float'" and I'm at my wits end.
My code looks like this:
def get_aws_account_usage_charges(df):
df_gb = df[['item_type', 'item_currency', 'item_description', 'item_price', 'item_quantity', 'charge_amount', 'item_amount']].groupby(['item_type', 'item_currency', 'item_description', 'item_price'], dropna=False).sum().reset_index()
format_aud = lambda conv: '%.2f' % (conv['item_amount'])
format_usd = lambda conv: '%.2f' % (conv['charge_amount'])
df_gb['charges_aud'] = df_gb.apply(format_aud, axis=1)
df_gb['charges_usd'] = df_gb.apply(format_usd, axis=1)
print(df_gb['charges_aud'])
return df_gb.sort_values(by='charge_amount', ascending=False)
The return looks like this:
0 1.11
1 17.00
2 0.70
3 0.59
4 0.00
5 0.05
6 2.45
7 6.00
8 0.00
9 0.00
10 0.00
11 3.60
12 0.13
13 0.00
14 0.01
15 0.10
16 0.04
17 0.00
18 0.00
19 0.01
20 0.01
21 0.10
22 0.01
Name: charges_aud, dtype: object
Any idea how I could remove the zeroes before I do my return, so when I do print(df_gb['charges_aud']), it'd look like this?
0 1.11
1 17.00
2 0.70
3 0.59
4 0.05
5 2.45
6 6.00
7 3.60
8 0.13
9 0.01
10 0.10
11 0.04
12 0.01
13 0.01
14 0.10
15 0.01
Solution 1:[1]
...
df_gb = df[['item_type', 'item_currency', 'item_description', 'item_price', 'item_quantity', 'charge_amount', 'item_amount']].groupby(['item_type', 'item_currency', 'item_description', 'item_price'], dropna=False).sum().reset_index()
df_gb = df_gb[df_gb.item_amount > 0]
format_aud = lambda conv: '%.2f' % (conv['item_amount'])
format_usd = lambda conv: '%.2f' % (conv['charge_amount'])
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hyena |
