Pandas column to list of strings

In this article, I will explain how to convert single or multiple pandas columns to string type, here, I will demonstrate using DataFrame.astype[str],DataFrame.values.astype[str], DataFrame.apply[str], DataFrame.map[str] and DataFrame.applymap[str] methods to covert any type to string type.

1. Quick Examples of Convert Columns To String

If you are in a hurry, below are some of the quick examples of how to convert columns to string in Pandas DataFrame. You can apply these to convert from/to any type in Pandas

Note that map[str] and apply[str]takes less time to compare with the remaining techniques.

# Below are quick example # Single columns string conversion df["Discount"] = df["Discount"].astype[str] print[df.dtypes] # Using DataFrame.values.astype[] to convert columns string df["Discount"]=df["Discount"].values.astype[str] print[df.dtypes] # Multiple columns string conversion df["Fee"] = df["Fee"].astype[str] df["Discount"]= df["Discount"].astype[str] print[df.dtypes] # Multiple columns string conversion df[['Courses', 'Fee', 'Duration','Discount']] = df[['Courses', 'Fee', 'Duration','Discount']].astype[str] print[df.dtypes] # Using apply[str] method df["Fee"]=df["Fee"].apply[str] print[df.dtypes] # Using apply[str] method to lambda function df["Fee"] = df["Fee"].apply[lambda x: str[x]] print[df.dtypes] # Convert columns string using map[str] method df['Fee'] = df["Fee"].map[str] print[df.dtypes] # Convert entire DataFrame to string df=df.applymap[str] print[df.dtypes]

Now, lets see a detailed example. first, create a Pandas DataFrame with a few rows and columns and execute and validate the results. Our DataFrame contains column names Courses, Fee, Duration, and Discount.

import pandas as pd import numpy as np technologies= [{ 'Courses':["Spark","PySpark","Hadoop","Python","Pandas","Hadoop","Spark"], 'Fee' :[22000,25000,23000,24000,26000,25000,25000], 'Duration':['30day','50days','55days','40days','60days','35day','55days'], 'Discount':[1000,2300,1000,1200,2500,1300,1400] }] df = pd.DataFrame[technologies] print[df] print[df.dtypes].

Yields below output.

Courses Fee Duration Discount 0 Spark 22000 30day 1000 1 PySpark 25000 50days 2300 2 Hadoop 23000 55days 1000 3 Python 24000 40days 1200 4 Pandas 26000 60days 2500 5 Hadoop 25000 35day 1300 6 Spark 25000 55days 1400 Courses object Fee int64 Duration object Discount int64 dtype: object

You can identify the data type of each column by using dtypes.

2. Convert Single Column to String Type Using DataFrame.astype[str]

Use df["Discount"]=df["Discount"].astype[str] method to change the data type of the Discount column from int64 to object type representing the string.

# Single columns string conversion df["Discount"] = df["Discount"].astype[str] print[df.dtypes]

3. Using DataFrame.values.astype[str] to Convert Columns String

You can also use df["Discount"]=df["Discount"].values.astype[str] method to change the data type of the Discount column from int64 to object type representing the string.

# Using DataFrame.values.astype[] to convert columns string df["Discount"]=df["Discount"].values.astype[str] print[df.dtypes]

Yields below output.

Courses object Fee int64 Duration object Discount object dtype: object

4. Convert Multiple Columns to String Using DataFrame.astype[str]

To convert multiple columns from list to strings, use df[['Courses','Fee','Duration','Discount']]=df[['Courses','Fee','Duration','Discount']].astype[str], this changes the data type of all columns from int64 to object type representing the string.

# Multiple columns string conversion df["Fee"] = df["Fee"].astype[str] df["Discount"]= df["Discount"].astype[str] print[df.dtypes] # Multiple columns string conversion df[['Courses', 'Fee', 'Duration','Discount']] = df[['Courses', 'Fee', 'Duration','Discount']].astype[str] print[df.dtypes]

Yields below output.

Courses object Fee object Duration object Discount object dtype: object

5. Using DataFrame.apply[str]

You can convert the column Fee to a string by simply using apply[str] , for example df["Fee"]=df["Fee"].apply[str] .

# Using apply[str] method df["Fee"]=df["Fee"].apply[str] print[df.dtypes]

Yields below output.

Courses object Fee object Duration object Discount int64 dtype: object

Using .apply[] with a lambda conversion function also works in this case. For example df["Fee"] = df["Fee"].apply[lambda x: str[x]].

# Using apply[str] to lambda function df["Fee"] = df["Fee"].apply[lambda x: str[x]] print[df.dtypes]

Yields same output as above.

6. Convert Columns to String Using DataFrame.map[str]

You can also convert the column Fee to a string by using map[str] , for example df['Fee']=df["Fee"].map[str].

# Convert columns string using map[str] method df['Fee'] = df["Fee"].map[str] print[df.dtypes]

Yields same output as above.

Note: map[str] and apply[str]takes less time to compare with the remaining techniques.

7. Convert Entire DataFrame to Strings Using DataFrame.applymap[str]

If you want to change the data type for all columns in the DataFrame to the string type, you can use df=df.applymap[str] method.

# Convert entire DataFrame to string df=df.applymap[str] print[df.dtypes]

Yields below output.

Courses object Fee object Duration object Discount object dtype: object

Conclusion

In this article, you have learned how to convert columns to string type in Pandas using DataFrame.astype[str], DataFrame.values.astype[str] and DataFrame.apply[str] methods. Also, you have learned how to convert columns to string in DataFrame using DataFrame.map[str], DataFrame.applymap[str] methods.

Happy Learning !!

You May Also Like

  • How to Select DataFrame Rows Based on Column Values in Pandas
  • Different Ways to Upgrade PIP Latest or to a Specific Version

References

  • //pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_string.html

Share this:

Video liên quan

Chủ Đề