I followed the following procedure: In Python, how do I convert all of the items in a list to floats? because each column of my Dataframe is list
, but instead of floats
I chose to change all the values to strings
.
df = [str(i) for i in df]
But this failed.
It simply erased all the data except for the first row of column names.
Then, trying df = [str(i) for i in df.values]
resulted in changing the entire Dataframe into one big list, but that messes up the data too much to be able to meet the goal of my script which is to export the Dataframe to my Oracle table.
Is there a way to convert all the items that are in my Dataframe that are NOT strings into strings?
With pandas >= 1.0 there is now a dedicated string datatype:
You can convert your column to this pandas string datatype using .astype('string'):
This is different from using
str
which sets the pandas 'object' datatype:You can see the difference in datatypes when you look at the info of the dataframe:
From the docs:
Information about pandas 1.0 can be found here:
https://pandas.pydata.org/pandas-docs/version/1.0.0/whatsnew/v1.0.0.html
You can use this:
out of curiosity I decided to see if there is any difference in efficiency between the accepted solution and mine.
The results are below:
example df:
test
df.astype
:test
df.applymap
:It seems
df.astype
is quite a lot faster :)You can use
applymap
method:This worked for me: