Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

set dtype for multiple columns pandas

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)
Comment

pandas change multiple column types

#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')
Comment

pandas assign multiple columns at once

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
Comment

assign multiple columns pandas

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
Comment

pandas assign multiple columns

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
Comment

PREVIOUS NEXT
Code Example
Python :: get columns that contain null values pandas 
Python :: ValueError: Shapes (None, 1) and (None, 11) are incompatible keras 
Python :: pandas dataframe convert string to float 
Python :: column contains substring python 
Python :: python count hex 
Python :: python insert object into list 
Python :: python pandas series to dataframe 
Python :: accessing dictionary elements in python 
Python :: extract text regex python 
Python :: chart-studio python install 
Python :: how to check if a number is a perfect square python 
Python :: popup window python tkinter 
Python :: python no such file python3 
Python :: how to open sound file in python 
Python :: gnome-shell turn off 
Python :: create 2d list dictionary 
Python :: python max value of list of tuples 
Python :: what is cleaned data in django 
Python :: how to print in pyhton 
Python :: python defaultdict 
Python :: python string to hex 
Python :: tensorfow list devices 
Python :: python inf 
Python :: flask render_template 
Python :: convert image to black and white python 
Python :: export csv 
Python :: how to shutdown a windows 10 computer using python 
Python :: multiple inputs in python 
Python :: python find index of minimum in list 
Python :: create virtual env 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =