所以我有一個 csv,我試圖通過
df = pd.read_csv("watchlist.csv", sep='\s{2,}',)
當我時它似乎作業正常 print(df)
此外,當我列印列時,這是我得到的輸出。
print(df.columns) #- OUTPUT:
Index([',Name,Growth,Recommendation,CurrentRatio,TotalCash,Debt,Revenue,PercentageSharesOut,PercentageInstitutions,PercentageInsiders,PricetoBook,ShortRatio,RegularMarketPrice'], dtype='object')
我遇到的問題是,當我嘗試去訪問帶有類似內容的列時
med_debt = math.floor(df.Debt), or even
print(df.Debt)
我收到一個屬性錯誤:
AttributeError: 'DataFrame' object has no attribute 'Debt'
這里的任何幫助都會受到贊賞
uj5u.com熱心網友回復:
sep='\s{2,}'引數將導致column 串列成為object的型別 的字串,例如:
>>> df = pd.read_csv("weather", sep='\s{2,}')
>>> df.columns
Index(['Date/Time,Temp (C),Dew Point Temp (C),Rel Hum (%),Wind Spd (km/h),
Visibility (km),Stn Press (kPa),Weather'], dtype='object')
>>> df.index
RangeIndex(start=0, stop=8784, step=1)
當您嘗試訪問特定列時, math.floor(df.Debt)它會回傳
AttributeError: 'DataFrame' object has no attribute 'Debt'
或者可能 df["Debt"]
raise KeyError(key) from err
(KeyError: 'Debt')
要df通過這種方式訪問特定列,請使用:
df = pd.read_csv("watchlist.csv")
uj5u.com熱心網友回復:
分隔符沒有正確分隔 csv,請嘗試將其省略并讓 csv 閱讀器使用默認值,。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/368891.html
