I have a pd DataFrame with integers displayed as strings:
frame = pd.DataFrame(np.random.randn(4, 3), columns=list('ABC'), index=['1', '2', '3', '4'])
frame = frame.apply(lambda x: x.astype(str))
This gives me a dataframe:
A B C
1 -0.890 0.162 0.477
2 -1.403 0.160 -0.570
3 -1.062 -0.577 -0.370
4 1.142 0.072 -1.732
If I type frame.type() I will get objects. Now I want to convert columns ['B':'C'] to numbers.
Imagine that I have dozens of columns and therefore I would like to slice them. So what I do is:
frame.loc[:,'B':'C'] = frame.loc[:,'B':'C'].apply(lambda x: pd.to_numeric(x, errors='coerce')
If I just wanted to alter column, say, B, I would type:
frame['B'] = frame['B'].apply(lambda x: pd.to_numeric(x, errors='coerce')
and that would convert B into into float64 BUT if I use it with .loc then nothing happens after I call DataFrame.info()!
Can someone help me? OF course I can just type all columns but I would like to get a more practical approach