How do I change the data type of a pandas Series?




[ad_1]

Have you ever tried to do math with a pandas Series that you thought was numeric, but it turned out that your numbers were stored as strings? In this video, I’ll demonstrate two different ways to change the data type of a Series so that you can fix incorrect data types. I’ll also show you the easiest way to convert a boolean Series to integers, which is useful for creating dummy/indicator variables for machine learning.

SUBSCRIBE to learn data science with Python:
https://www.youtube.com/dataschool?sub_confirmation=1

JOIN the “Data School Insiders” community and receive exclusive rewards:
https://www.patreon.com/dataschool

== RESOURCES ==
GitHub repository for the series: https://github.com/justmarkham/pandas-videos
“astype” documentation: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.astype.html

== LET’S CONNECT! ==
Newsletter: https://www.dataschool.io/subscribe/
Twitter: https://twitter.com/justmarkham
Facebook: https://www.facebook.com/DataScienceSchool/
LinkedIn: https://www.linkedin.com/in/justmarkham/

Source


[ad_2]

Comment List

  • Data School
    December 1, 2020

    Starting in pandas version 0.19, you can change the data type of multiple columns at once! Learn how to do it here: https://www.youtube.com/watch?v=-NbY7E9hKxk&list=PL5-da3qGB5ICCsgW1MxlZ0Hq8LL5U3u9y&index=32

  • Data School
    December 1, 2020

    hi
    can you guide me how to convert object type 68+2 in to integer or float

  • Data School
    December 1, 2020

    literally just tried this with a series of objects that I want to be integers and it didn't work. what gives

  • Data School
    December 1, 2020

    Dude, thnks for your help!!!

  • Data School
    December 1, 2020

    Your videos are informative for a beginner like me in Python. More power to you.

  • Data School
    December 1, 2020

    Hello
    I have a data set (json), it has a column named 'price'. It is showing as object and when I am trying to find out the mean of the column 'price' using your method – data.price.str.replace('$', ' ').astype(float).mean()
    It is showing error, please help me out
    Thanks

  • Data School
    December 1, 2020

    when writing `dtype={'beer_servings': float}`, why doesn't `float` have to be within quotations (as a string) or otherwise imported?

  • Data School
    December 1, 2020

    You're brilliant!

  • Data School
    December 1, 2020

    Thank you! 🙂

  • Data School
    December 1, 2020

    how do I convert numbers in a column to percentage and in-place it into dataframe?

  • Data School
    December 1, 2020

    Given that `.str.replace` defaults to regex, why does `.str.replace('$', '')` work? It seems like it should interpret `$` as end-of-line.

  • Data School
    December 1, 2020

    Thank you for these videos….the explanation is top notch.

    While converting the item_price to a float (code line 12) did return an average of 7.46, item_price still shows up as object (ran .dtypes command). This is not what i expected. Any reason why this is the case?

  • Data School
    December 1, 2020

    Sir,when we replace elements from a series, operation is done but still the dataframe is same ie I can't see the changes on CSV files? I guess it needs a variable to Store the operations?

  • Data School
    December 1, 2020

    thanks for such a clear explanation

  • Data School
    December 1, 2020

    When i want to read the csv file with sep (;) and convert the 'Height' to float from the start… i try this … dsv = pd.read_csv(('c:/users/dsv/desktop/LungCapData.csv',sep';'), dtype={'Height':float}) … but …Error.

  • Data School
    December 1, 2020

    Thank you so much!

  • Data School
    December 1, 2020

    When am converting objective data type to integer

    There is error showing base 10:. '100,00'

    Why so???Plz help me solve this issue

  • Data School
    December 1, 2020

    How to change a float column containing NaN values to int column by keeping NaN values as it is?

  • Data School
    December 1, 2020

    well done. thank you man

  • Data School
    December 1, 2020

    u did not show the dataframe after doing the astype

  • Data School
    December 1, 2020

    How can we convert the data type of item_price inplace(as in the original dataset)? I did not find inplace argument in astype method . Please help me on this.

  • Data School
    December 1, 2020

    if the column contain non numeric data and if i need to convert them to 0 or NaN and the rest to float, how should I do. In this case the data may not have a certain character like $, instead say there are non valid numbers.
    ex. "10:00", "5.6.7", "> 50"

  • Data School
    December 1, 2020

    Firstly, thank you for your videos, they are very helpful. Secondly, I have a question. I haven't watched all your videos yet, so, maybe you have an answer somewhere already (if yes, could you please share a link). So, my question is how to convert floats to integers. I was given a task to carry out the analysis of gun ownership data, and the number of guns is always shown as a float, which is okay, you can still conduct numerical operations, but logically, you don't need the number of guns to be shown as floats, because you cannot own 1.5 or 4.75 guns, right? So, the dataframe just visually does not seem nice with all unnecessary .0 's. I was trying to use .astype(int), but for some reasons was getting errors, maybe I'm doing something wrong? Please advise.

  • Data School
    December 1, 2020

    How can i convert non-numeric data to numerical data. can you make a video regarding this.

  • Data School
    December 1, 2020

    hello just gone through the video.Its really very helpful. Ty soo for this help
    well i am facing an issue with this particular code.

    orders['item_price']=(orders.item_price.str.replace('$','').astype(float))

    orders.head()

    its showing error, i want to permanently change the item_price column. can someone pls help?

  • Data School
    December 1, 2020

    Just what I needed! Great explanation and visuals! Subscribed!

  • Data School
    December 1, 2020

    i really like your videos and plaease help me out on this
    ValueError: Input contains NaN, infinity or a value too large for dtype('float64'). am using Jupyter Notebook

  • Data School
    December 1, 2020

    Hi can someone help .I am getting this error "ValueError: Cannot convert non-finite values (NA or inf) to integer
    " while using this code inp0['age'] = inp0.age.astype(int). The dtype if float for the age column.

  • Data School
    December 1, 2020

    you are super cool

  • Data School
    December 1, 2020

    How to convert object type into int?

  • Data School
    December 1, 2020

    Hi everyone. I HAVE A GREAT EXERCISE FOR PANDAS LEARNERS. HOW CAN I GET ONLY CHICKEN BOWL FROM ITEM_NAME AND WITH THE PRICE ONLY ABOVE $10 FROM ITEM_PRICE. by the way this exercise uses order-csv file data which is shown in previous 12th video tutorial. Thank u so much. Please do not forget to write the answer below

  • Data School
    December 1, 2020

    Very useful video, helps me out of many bugs, thanks for sharing.

  • Data School
    December 1, 2020

    If I show you a CSV would you recommend me some tips to convert it into INT from Object DTYPE ?

  • Data School
    December 1, 2020

    thanks for the video. but i have a question, in some cases as type() didn't work.we need to use pd.to_numeric to change the data type of any series.could you brief us about this. where do i use as type to change the data type and where do i use pd.to_numeric to change the data type.

  • Data School
    December 1, 2020

    I tried to convert date to float type. It looks values are changed.
    For eg:-31st may changes to 1.45
    Am I on the right path

  • Data School
    December 1, 2020

    when to use dtype and when dtypes?

    drinks = pd.read_csv('http://bit.ly/drinksbycountry',dtype = {'beer_servings': float})
    drinks.dtypes

    in this code we use d type in first row while in 2nd row its dtypes.
    its bit confusing

  • Data School
    December 1, 2020

    Can you make some videos about regular expression in python? That's so hard and I can't find any good video to teach it.

  • Data School
    December 1, 2020

    Learning a lot from your videos. Thank you so much.

  • Data School
    December 1, 2020

    Please post a video on cleaning the columns of a csv file. Like removing $ or translate 19K into a common format. Thanks

  • Data School
    December 1, 2020

    hey! how did you accessing png image. ![ ](name.png) it's not going to work with me.

  • Data School
    December 1, 2020

    Hello
    i have dataset in which one numeric column is in string so how can we change it to integer as i'm trying to do it but facing below error ValueError: could not convert string to float: '#VALUE!' or this ValueError: invalid literal for int() with base 10: '#VALUE! …..

  • Data School
    December 1, 2020

    Great Videos! So when to use Dictionary over List in Pandas? I am trying to read .xls file after extracting from zip file, and then commit the data to a Database. What would be the best way to do such an event? Any help would be of great help!

Write a comment