How to convert multiple columns to string in pandas dataframe

0 votes

I have dataframes with different datatypes(code is given below). 


repair['SERVICE_TYPE']= repair['SERVICE_TYPE'].astype(str)

I want a function that would help me pass multiple columns and convert them into strings.

Jul 19, 2019 in Python by Shabnam
• 930 points

1 answer to this question.

0 votes


To convert multiple columns to string, include a list of columns to your above-mentioned command:

df[['one', 'two', 'three']] = df[['one', 'two', 'three']].astype(str)

# you can add any number of columns

To convert all columns into string, you need to construct the list of columns:

all_columns = list(df) # Creates list of all column headers
df[all_columns] = df[all_columns].astype(str)
answered Jul 19, 2019 by Taj
• 1,080 points
Thank you so much for sharing info..this saved lot of my time.

Hi, @There,

Thanks for your contribution. Please do upvote the answer in case you found it helpful!

Related Questions In Python

0 votes
1 answer

How to add multiple columns to pandas dataframe in one assignment?

Hello @kartik, You could use assign with a dict of ...READ MORE

answered Jun 15, 2020 in Python by Niroj
• 82,880 points
0 votes
1 answer

How to convert a Pandas GroupBy object to DataFrame in Python

g1 here is a DataFrame. It has a hierarchical index, ...READ MORE

answered Nov 12, 2018 in Python by Nymeria
• 3,540 points
0 votes
1 answer

How to convert string in Pandas Series to lower case?

Suppose you have the series stored in ...READ MORE

answered May 13, 2019 in Python by Raj
+1 vote
1 answer

How to change the order of DataFrame columns in pandas?

Hi@akhtar, You can rearrange a DataFrame object by ...READ MORE

answered Oct 20, 2020 in Python by MD
• 95,320 points
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,486 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
0 votes
1 answer

How to iterate over row in a Dataframe in Pandas?

Hi, You can use df.iterrows(), it yields both ...READ MORE

answered Jul 19, 2019 in Python by Taj
• 1,080 points
0 votes
1 answer

How to rename columns in pandas?

Hi,  To rename columns in pandas, you can ...READ MORE

answered Aug 9, 2019 in Python by Taj
• 1,080 points