【发布时间】:2016-12-21 16:19:58
【问题描述】:
我有一个数据框 energy 在某些列中缺少值。缺失值由数据框中的字符串 ... 表示。我想用np.NaN替换所有这些值
In [3]: import pandas as pd
In [4]: import numpy as np
In [7]: energy = pd.read_excel('test.xls', skiprows = 17, skip_footer = 38, parse_cols = range(2, 6), index_col = None, names = ['Country', 'ES'
...: , 'ESC', '% Renewable'])
In [8]: energy[(energy['ES'] == "...") | (energy['ESC'] == "...")]
Out[8]:
Country ES ESC % Renewable
3 American Samoa ... ... 0.641026
86 Guam ... ... 0.000000
150 Northern Mariana Islands ... ... 0.000000
210 Tuvalu ... ... 0.000000
217 United States Virgin Islands ... ... 0.000000
为了替换这些值,我尝试了:
In [9]: energy[(energy['ES'] == "...")]['ES'] = np.NaN
/usr/local/bin/ipython:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
#!/usr/bin/python3
我不明白这个错误,我也没有看到任何其他方法来实现我想要的。有什么想法吗?
【问题讨论】:
-
仅供参考,您可以将
na_values='...'传递给pd.read_excel。 -
重新打开因为
also I don't see any other way to achieve what I want to -
@jezrael 这有什么关系?
-
我不确定,但似乎有一半是重复的。
标签: python python-3.x pandas dataframe missing-data