我有一个苹果库存数据的数据框,而关闭列中有一些“空”字符串。我无法绘制我的数据,因为我不断收到'无法将字符串转换为...'错误。当我尝试下面的代码时,我收到错误' AttributeError:'DataFrame'对象没有属性'close_arr'。此代码基于此帖子How to drop rows from pandas data frame that contains a particular string in a particular column?
中提供的答案apl = pd.read_csv(path, index_col='Date', parse_dates=True)
close_arr = aapl['Close']
aapl = aapl[~aapl.close_arr.str.contains("null")]
aapl.head()
这是info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 9234 entries, 1980-12-12 to 2017-07-26
Data columns (total 6 columns):
Adj Close 9234 non-null object
Close 9234 non-null object
High 9234 non-null object
Low 9234 non-null object
Open 9234 non-null object
Volume 9234 non-null object
dtypes: object(6)
memory usage: 505.0+ KB
我也尝试过:
aapl = aapl[~aapl['Close'].str.contains("null")]
我在哪里错了?
答案 0 :(得分:2)
尝试使用df.replace
后跟df.dropna
:
aapl = aapl.replace('null', np.nan).dropna()