How do you convert columns in python?
Use pandas Show
Note that while converting a float to int, it doesn’t do any rounding and flooring and it just truncates the fraction values (anything after .). In this article, I will explain different ways to convert columns with float values to integer values. If you are in a hurry, below are some of the quick examples of how to convert column to integer dtype in DataFrame.
Now, let’s create a DataFrame with a few rows and columns, execute some examples and validate the results. Our DataFrame contains column names
Yields below output. Note that Fee column is string/object hilding integer value and Discount is float64 type.
2. Convert Column to int (Integer)Use pandas DataFrame.astype() function to convert column to int (integer), you can apply this on a specific column or on an entire DataFrame. To cast the data type to 64-bit signed integer, you can
use The Below example converts
Yields below output.
If you have a DataFrame that has all string columns holiding integer values, you can convert it to int dtype simply using as below. If you have any column that has alpha-numeric values, this returns an error. If you run this on our DataFrame, you will get an error.
You can also use Series.astype() to convert a specific column. since each column on DataFrame is pandas Series, I will get the column from DataFrame as Series and use
3. Convert Float to Int dtypeNow by using the same approaches using astype() let’s convert the float column to int (integer) type in pandas DataFrame. Note that while converting a float to int, it doesn’t do any rounding and flooring and it just truncates the fraction values (anything after .). The below example, converts column
Yields below output
Similarly, you can also cast all columns or a single columns. Refer examples for above section for details. 4. Casting Multiple Columns to IntegerYou can also convert multiple columns to integer by sending dict of column name -> data type to
Yields below output.
5. Using apply(np.int64) to Cast to IntegerYou can
also use DataFrame.apply() method to convert
Yields below output.
6. Convert Column Containing NaNs to astype(int)In order to demonstrate some
Use DataFrame.fillna() to replace the NaN values with integer value zero.
Yields below output.
ConclusionIn this article, you have learned how to convert column string to int, float to to int using DataFrame.astype() and DataFrame.apply() method. Also, you have learned how to convert float and string to integers when you have Nan/null values in a column. Happy Learning !! You May Also Like
References
How do I convert multiple columns to one column in Python?Step #1: Load numpy and Pandas. Step #2: Create random data and use them to create a pandas dataframe. Step #3: Convert multiple lists into a single data frame, by creating a dictionary for each list with a name. Step #4: Then use Pandas dataframe into dict.
How do I convert a column to text in Python?Use pandas DataFrame. astype() function to convert a column from int to string, you can apply this on a specific column or on an entire DataFrame. The Below example converts Fee column from int to string dtype.
How do you convert columns to time format in Python?Use pandas to_datetime() function to convert the column to DateTime on DataFrame. Use the format parameter of this method to specify the pattern of the DateTime string you wanted to convert.
How do you convert a column of data into a list in Python?Use the tolist() Method to Convert a Dataframe Column to a List. A column in the Pandas dataframe is a Pandas Series . So if we need to convert a column to a list, we can use the tolist() method in the Series . tolist() converts the Series of pandas data-frame to a list.
|