大熊猫fillna不起作用

时间:2018-03-12 16:50:07

标签: python pandas

我不太确定我做错了什么,但是我无法使用fillna()来处理我的数据帧。这是片段:

print(featureframe.mean())
featureframe = featureframe.fillna(featureframe.mean().to_dict())
if featureframe.isnull().values.any():
    print("Still NaN in data")
    print(featureframe[featureframe.isnull().any(axis=1)])
    exit(9)

我检查了第一个打印输出:没有任何方法是NaN,所以第二行应该用NaN的每个元素填充列的平均值并消除我的任何NaNs数据集。但是,它会在后续的if子句中退出并打印很多仍然包含NaN值的行。

2 个答案:

答案 0 :(得分:1)

Traceback (most recent call last): File "myBot.py", line 21, in <module> print(reddit.user.me()) File "c:\Python27\lib\site-packages\praw\models\user.py", line 60, in me user_data = self._reddit.get(API_PATH['me']) File "c:\Python27\lib\site-packages\praw\reddit.py", line 367, in get data = self.request('GET', path, params=params) File "c:\Python27\lib\site-packages\praw\reddit.py", line 472, in request params=params) File "c:\Python27\lib\site-packages\prawcore\sessions.py", line 181, in reques t params=params, url=url) File "c:\Python27\lib\site-packages\prawcore\sessions.py", line 124, in _reque st_with_retries retries, saved_exception, url) File "c:\Python27\lib\site-packages\prawcore\sessions.py", line 90, in _do_ret ry params=params, url=url, retries=retries - 1) File "c:\Python27\lib\site-packages\prawcore\sessions.py", line 124, in _reque st_with_retries retries, saved_exception, url) File "c:\Python27\lib\site-packages\prawcore\sessions.py", line 90, in _do_ret ry params=params, url=url, retries=retries - 1) File "c:\Python27\lib\site-packages\prawcore\sessions.py", line 112, in _reque st_with_retries data, files, json, method, params, retries, url) File "c:\Python27\lib\site-packages\prawcore\sessions.py", line 97, in _make_r equest params=params) File "c:\Python27\lib\site-packages\prawcore\rate_limit.py", line 32, in call kwargs['headers'] = set_header_callback() File "c:\Python27\lib\site-packages\prawcore\sessions.py", line 141, in _set_h eader_callback self._authorizer.refresh() File "c:\Python27\lib\site-packages\prawcore\auth.py", line 328, in refresh password=self._password) File "c:\Python27\lib\site-packages\prawcore\auth.py", line 138, in _request_t oken response = self._authenticator._post(url, **data) File "c:\Python27\lib\site-packages\prawcore\auth.py", line 29, in _post data=sorted(data.items())) File "c:\Python27\lib\site-packages\prawcore\requestor.py", line 49, in reques t raise RequestException(exc, args, kwargs) prawcore.exceptions.RequestException: error with request ("bad handshake: Error( [('SSL routines', 'tls_process_server_certificate', 'certificate verify failed') ],)",) 是否包含任何也具有NaN值的非数字列? featureframe将从平均计算中排除这些,因此没有相应的条目会导致字典传递给DataFrame.mean,这意味着非数字列NaN将保留。

如果数据都是数字,那么我可以说我无法重现你的问题。对于包含所有数字列的fillna,您使用的代码没有错误,并使用相应的列均值填充所有NaN值。这是一个例子:

DataFrame

答案 1 :(得分:-1)

我仍然不知道fillna有什么问题,但这是一个对我有用的解决方法:

for c in featureframe:
    featureframe[c] = featureframe[c].replace(np.nan, featureframe[c].mean())

这样做似乎远非理想,但至少它填补了NaN。