AttributeError: 'DataFrame' object has no attribute 'price' in Python

Dung Do Tien Jul 02 2022 203

Hello Pythoner guys. I am a newbie in Python and I want to read data from an scv file. I used pandas module to do it. You can see here:

The main.py file:

import pandas as pd

df = pd.read_csv('books.csv')
df.price = df.price.str.replace('£','').astype(float)
print(df.price)

The books.csv file:

    name   price
0  Clearn Code   £7.09
1  Pro SQL Server   £8.10
2  CSS & HTML5  £10.19
3  Java OOP   £24.01

I want to read the price column in csv file and print out it. But it threw an exception AttributeError: 'DataFrame' object has no attribute 'price'. The full message here:

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    df.name = df.name.str.replace('£','').astype(float)
  File "/usr/lib/python3.8/site-packages/pandas/core/generic.py", line 5130, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'price'

I used Python version 3.10 and Window 11.

Any solutions? Thanks in advance.

Have 1 answer(s) found.
  • S

    Sachin Joshi Jul 03 2022

    Hi guys, I think you wrong format csv file. To separate column in csv you have to use comma not space.  For example:

    name,price
    Clearn Code,£7.09
    Pro SQL Server,£8.10
    CSS & HTML5,£10.19
    Java OOP,£24.01

    And here is output:

    0     7.09
    1     8.10
    2    10.19
    3    24.01
    Name: price, dtype: float64

    I hope this answer will helpful to you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close