简体   繁体   中英

Why is this code throwing an error in pandas?

I solve this problem on Stepik: The data frame with the name my_stat contains data with 4 columns: session_value, group, time, n_users. In the variables n_users, we replaced all negative values with the median value of n_users (excluding negative values, of course). Here's what I wrote (I'm new to pandas):

import pandas as pd    
import numpy as np

my_stat = my_stat['session_value'].replace(np.nan, 0)
my_stat.loc[my_stat['n_users'] < 0, 'n_users'] = my_stat['n_users'].median()

But when posting the response, I get this error:

Error:
Traceback (most recent call last):
  File "jailed_code", line 25, in <module>
    med = my_stat['n_users'].median()
  File "/home/stepic/instances/master-plugins/sandbox/python3/lib/python3.6/site-packages/pandas/core/series.py", line 871, in __getitem__
    result = self.index.get_value(self, key)
  File "/home/stepic/instances/master-plugins/sandbox/python3/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 4405, in get_value
    return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
  File "pandas/_libs/index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value
  File "pandas/_libs/index.pyx", line 90, in pandas._libs.index.IndexEngine.get_value
  File "pandas/_libs/index.pyx", line 135, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index_class_helper.pxi", line 109, in pandas._libs.index.Int64Engine._check_type
KeyError: 'n_users'

How to fix it?

Change your line

my_stat = my_stat['session_value'].replace(np.nan, 0)

to this

my_stat['session_value'] = my_stat['session_value'].replace(np.nan, 0)

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.

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