Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert multiple columns to string in pandas dataframe

Tags:

I have a pandas data frame with different data types. I want to convert more than one column in the data frame to string type. I have individually done for each column but want to know if there is an efficient way?

So at present I am doing something like this:

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

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

like image 210
Sayonti Avatar asked Jun 13 '18 22:06

Sayonti


2 Answers

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) # add as many column names as you like. 

That means that one way to convert all columns is to construct the list of columns like this:

all_columns = list(df) # Creates list of all column headers df[all_columns] = df[all_columns].astype(str) 

Note that the latter can also be done directly (see comments).

like image 172
sudonym Avatar answered Sep 19 '22 12:09

sudonym


I know this is an old question, but I was looking for a way to turn all columns with an object dtype to strings as a workaround for a bug I discovered in rpy2. I'm working with large dataframes, so didn't want to list each column explicitly. This seemed to work well for me so I thought I'd share in case it helps someone else.

stringcols = df.select_dtypes(include='object').columns df[stringcols] = df[stringcols].fillna('').astype(str) 

The "fillna('')" prevents NaN entries from getting converted to the string 'nan' by replacing with an empty string instead.

like image 34
Joe Avatar answered Sep 21 '22 12:09

Joe