0

I have a large list of data, but one column I am filtering by (sales) has some missing data. It seems that I am getting an error with the function I made to convert dollar abbreviations (110M to 110,000,000) when here are missing values in the list. The error I get is "TypeError: 'int' object is not subscriptable." So I am trying to replace all missing values in this column with 0 so that when I run the function, it will iterate through every value in the column and if I filter so that sales > certain value, it will just filter those rows out.

Here an example of this:

def num_conv(sales_list):
    m = {'M': 6, 'B': 9}
    return ([(float(i[:-1]) * 10 ** m[i[-1]]) for i in sales_list])

sales = ['100M', '100B', None, '200M']

sales = sales.replace(np.nan, 0)
sales = num_conv(sales)
print(sales)

MaxDawg27
  • 71
  • 10
  • Does this answer your question? https://numpy.org/doc/stable/reference/generated/numpy.nan_to_num.html – Frederick Feb 06 '21 at 12:30
  • Does this answer your question? [How to replace NaN values by Zeroes in a column of a Pandas Dataframe?](https://stackoverflow.com/questions/13295735/how-to-replace-nan-values-by-zeroes-in-a-column-of-a-pandas-dataframe) – Vishnudev Krishnadas Feb 06 '21 at 12:30
  • The error is actually in `num_conv` itself. If `sales_list` is a list as shown in your example, then calling `for i in sales_list` makes `i` a single value, which must be either a list or a `str` for `i[-1]` to work. If you'll try to replace all missing values with zero (an `int`), your code will fail on the `TypeError` shown above. – Captain Trojan Feb 06 '21 at 12:33
  • @CaptainTrojan, if `i` is a string, then `i[-1]` is perfectly fine. – ForceBru Feb 06 '21 at 12:34
  • BTW, `None` is not the same as NaN: NaN is a floating-point value, while `None` is `NoneType`. – ForceBru Feb 06 '21 at 12:35
  • @ForceBru My bad, fixed, thx. – Captain Trojan Feb 06 '21 at 12:37

2 Answers2

0

Actually, you can do this by modifying your list comprehension:

def num_conv(sales_list):
    m = {'M': 6, 'B': 9}
    return ([(float(i[:-1]) * 10 ** m[i[-1]]) if i else 0 for i in sales_list])

sales = ['100M', '100B', None, '200M']
sales = num_conv(sales)
print(sales)

And the output will be

[100000000.0, 100000000000.0, 0, 200000000.0]
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
0

Do simple.

# python (simple)
def num_conv(sales_list):
    m = {'M': 6, 'B': 9}
    return ([(float(i[:-1]) * 10 ** m[i[-1]]) for i in sales_list])

sales = ['100M', '100B', None, '200M']
sales_li = []
for sale in sales:
    if sale:
        sales_li.append(sale)
    else:
        sales_li.append('0M')

sales = num_conv(sales_li)
print(sales)
CPMaurya
  • 371
  • 3
  • 9