kitty
0
Q:

set dtype for multiple columns pandas

# importing pandas as pd 
import pandas as pd 
  
# sample dataframe 
df = pd.DataFrame({ 
    'A': [1, 2, 3, 4, 5], 
    'B': ['a', 'b', 'c', 'd', 'e'], 
    'C': [1.1, '1.0', '1.3', 2, 5] }) 
  
# converting all columns to string type 
df = df.astype(str) 
print(df.dtypes) 
2
import pandas as pd

df = pd.DataFrame({'id':['a1', 'a2', 'a3', 'a4'],
  				   'A':['0', '1', '2', '3'],
                   'B':['1', '1', '1', '1'],
                   'C':['0', '1', '1', '0']})

df[['A', 'B', 'C']] = df[['A', 'B', 'C']].apply(pd.to_numeric, axis = 1)
0

New to Communities?

Join the community