Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 614
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 6
Kenil Vasani
Asked: December 14, 20202020-12-14T21:07:15+00:00 2020-12-14T21:07:15+00:00In: Python

Type Conversion in python AttributeError: ‘str’ object has no attribute ‘astype’

  • 6

I am confused by the type conversion in python pandas

df = pd.DataFrame({'a':['1.23', '0.123']})
type(df['a'])
df['a'].astype(float)

Here df is a pandas series and its contents are 2 strings, then I can apply astype(float) on this pandas series, and it correctly convert all string into float. However

df['a'][1].astype(float)

gives me AttributeError: ‘str’ object has no attribute ‘astype’. My question is: how can that be? I could convert the whole series from string to float but I couldn’t convert the entry of this series from string to float?

Also, I load my raw data set

df['id'].astype(int)

it generates ValueError: invalid literal for int() with base 10: ”
This one seems to suggest that there is a blank in my df['id']. So I check whether it is true by typing

'' in df['id']

it says false. So I am very confused.

pandaspythontype-conversion
  • 1 1 Answer
  • 13 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-14T21:02:23+00:00Added an answer on December 14, 2020 at 9:02 pm

      df['a'] returns a Series object that has astype as a vectorized way to convert all elements in the series into another one.

      df['a'][1] returns the content of one cell of the dataframe, in this case the string '0.123'. This is now returning a str object that doesn’t have this function. To convert it use regular python instruction:

      type(df['a'][1])
      Out[25]: str
      
      float(df['a'][1])
      Out[26]: 0.123
      
      type(float(df['a'][1]))
      Out[27]: float
      

      As per your second question, the operator in that is at the end calling __contains__ against the series with '' as argument, here is the docstring of the operator:

      help(pd.Series.__contains__)
      Help on function __contains__ in module pandas.core.generic:
      
      __contains__(self, key)
          True if the key is in the info axis
      

      It means that the in operator is searching your empty string in the index, not the contents of it.

      The way to search your empty strings is to use the equal operator:

      df
      Out[54]: 
          a
      0  42
      1    
      
      '' in df
      Out[55]: False
      
      df==''
      Out[56]: 
             a
      0  False
      1   True
      
      df[df['a']=='']
      Out[57]: 
        a
      1  
      
      • 5
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      ERROR: torch has an invalid wheel, .dist-info directory not found

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com