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)
#Two ways to do this
df[['parks', 'playgrounds', 'sports']].apply(lambda x: x.astype('category'))
cols = ['parks', 'playgrounds', 'sports', 'roading']:
public[cols] = public[cols].astype('category')
df = pd.DataFrame({'col1':[0,1,2], 'col2':[0,1,2], 'col3':[0,1,2]})
df
col1 col2 col3
0 0 0 0
1 1 1 1
2 2 2 2
df['col1'], df['col2'], df['col3'] = zip(*df.apply(lambda r: (1, 2, 3), axis=1))
df
col1 col2 col3
0 1 2 3
1 1 2 3
2 1 2 3
import pandas as pd
df = {'col_1': [0, 1, 2, 3],
'col_2': [4, 5, 6, 7]}
df = pd.DataFrame(df)
df[[ 'column_new_1', 'column_new_2','column_new_3']] = [np.nan, 'dogs',3] #thought this wo
In [1069]: df.assign(**{'col_new_1': np.nan, 'col2_new_2': 'dogs', 'col3_new_3': 3})
Out[1069]:
col_1 col_2 col2_new_2 col3_new_3 col_new_1
0 0 4 dogs 3 NaN
1 1 5 dogs 3 NaN
2 2 6 dogs 3 NaN
3 3 7 dogs 3 NaN