【发布时间】:2022-01-05 22:12:54
【问题描述】:
所以我有一个 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'
如有任何帮助,我们将不胜感激
【问题讨论】:
-
您的 DataFrame 没有名为
Debt的列。它也没有名称为Name、Growth等的列。它有 one 列,其名称为,Name,Growth,Recommendation,CurrentRatio,TotalCash,Debt,Revenue,PercentageSharesOut,PercentageInstitutions,PercentageInsiders,PricetoBook,ShortRatio,RegularMarketPrice。这是因为在读取 CSV 文件时,\s{2,}是用于sep的错误值。我猜你的意思是\s{2},。 -
如果你只是做
print(df)来判断你的列的输出,输出看起来像什么,看起来只有一列只是列名的字符串组合。 -
@KarlKnechtel 我想我添加它是为了尝试解决不同的问题,我会删除它,谢谢
标签: python pandas dataframe csv