I have a Pandas Dataframe which has columns which look something like this:
df:
Column0 Column1 Column2
'MSC' '1' 'R2'
'MIS' 'Tuesday' '22'
'13' 'Finance' 'Monday'
So overall, in these columns are actual strings but also numeric values (integers) which are in string format.
I found this nice post about the pd.to_numeric
and astype()
methods, but I can't see if or how I could use them in my case.
Using:
pd.to_numeric(df, errors = 'ignore')
just results in skiping the whole columns. Instead of skipping the whole columns, I only want to skip the strings in those columns which can't be converted, move on to the next entry and try to convert the next string.
So in the end, my dataframe would look like this:
df:
Column0 Column1 Column2
'MSC' 1 'R2'
'MIS' 'Tuesday' 22
13 'Finance' 'Monday'
Is there maybe an efficient way to loop over these columns and achieve that?
Best regards, Jan
EDIT: Thanks for all your suggestions! Since I am still a python beginner, @coldspeed and @sacul 's answers are easier to understand for me so I will go with one of them!