Understanding and Fixing Python’s TypeError: ‘float’ object is not subscriptable

-

Understanding and Fixing Python’s TypeError: ‘float’ object is not subscriptable

While working with Python data structures or pandas, you might encounter the following error:

TypeError: 'float' object is not subscriptable

This error often appears in niche situations—such as iterating over DataFrame columns with apply—and can be confusing because search results are scarce. This article explains what “not subscriptable” means and shows how to resolve the error.

Why does this error occur?

In Python, certain objects like lists, strings and dictionaries are subscriptable, meaning you can use square‑bracket notation (obj[index]) to access elements. Numbers (int, float) are not subscriptable. As the DS100 debugging guide explains, a TypeError arises when you try to index into an integer or float. For example:

x = 3.14
print(x[0])  # ❌ raises TypeError: 'float' object is not subscriptable

Here, x is a float and cannot be indexed. This rule also applies when your variables unexpectedly hold a float. A common scenario is using pandas.DataFrame.apply() incorrectly: apply often returns a scalar (e.g., a float), and if you try to index that scalar, you’ll see the same error.

Example: erroneous apply usage

Suppose you have a DataFrame df with columns target_sum, target_count and target. You want to compute the mean of the remaining targets excluding the current row. An incorrect approach would be:

# ❌ Wrong approach using apply
df['item_id_mean_target'] = (
    df.apply(lambda r: (r['target_sum'] - r['target']) / (r['target_count'] - 1))[0]
)

The lambda returns a single float for each row, so the result of apply is a Series of floats. Trying to index the result with [0] raises TypeError: 'float' object is not subscriptable. The accepted Stack Overflow answer recommends a vectorized approach instead:

# ✅ Vectorized solution
df['item_id_mean_target'] = (
    df['target_sum'] - df['target']
) / (
    df['target_count'] - 1
)

This calculation operates on entire columns and returns a Series directly, so there is no need for indexing and no TypeError.

How to fix the error

  1. Check your variable types. When you see 'float' object is not subscriptable, inspect the variable causing the error. Use print(type(x)) or type(r) inside your function to verify it is not a list or other iterable. If it’s a float, remove the indexing operation. The DS100 guide suggests this as the first debugging step.
  2. Avoid indexing scalar values. Don’t use [0] or [i] on objects that are floats. If you need to access multiple values, ensure your code returns a list or Series instead of a single number.
  3. Use vectorized operations in pandas. Instead of apply with a lambda that returns a scalar, rewrite your calculation to operate on entire columns. The example above uses column arithmetic to compute the mean without indexing. Vectorized operations are more efficient and less error‑prone.
  4. Convert floats to iterables (if appropriate). If you truly need to subscript a float, convert it to a string or list. For example: str(x)[0] will return the first character of the float’s string representation. However, this is rarely the correct solution for numeric computations.

Conclusion

The TypeError: 'float' object is not subscriptable occurs when you inadvertently treat a number like a sequence. It often appears in pandas when misusing apply. To fix it, check your variable types, remove invalid indexing and use vectorized operations. With these steps, you can avoid this error in future Python projects

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent comments