[英]Dropping rows with specific string value Pandas
我有一個蘋果股票數據的數據框,並且“關閉”列中有一些“空”字符串。 我無法繪制數據,因為我不斷收到“無法將字符串轉換為...”錯誤。 當我嘗試下面的代碼時,出現錯誤' AttributeError:'DataFrame'對象沒有屬性'close_arr' 。 這段代碼基於該線程中提供的答案。 如何從熊貓數據框中刪除包含特定列中特定字符串的行?
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")]
我在哪里錯了?
嘗試先使用df.replace
使用df.dropna
:
aapl = aapl.replace('null', np.nan).dropna()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.