Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas create column from another column

# Creates a new column 'blue_yn' based on the existing 'color' column
# If the 'color' column value is 'blue' then the new column value is 'YES'
df['blue_yn'] = np.where(df['color'] == 'blue', 'YES', 'NO')
# Can also do this using .apply and a lambda function
df['blue_yn']= df['color'].apply(lambda x: 'YES' if (x == 'blue') else 'NO') 
Comment

create new columns pandas from another column

def label_race (row):
   if row['eri_hispanic'] == 1 :
      return 'Hispanic'
   if row['eri_afr_amer'] + row['eri_asian'] + row['eri_hawaiian'] + row['eri_nat_amer'] + row['eri_white'] > 1 :
      return 'Two Or More'
   if row['eri_nat_amer'] == 1 :
      return 'A/I AK Native'
   if row['eri_asian'] == 1:
      return 'Asian'
   if row['eri_afr_amer']  == 1:
      return 'Black/AA'
   if row['eri_hawaiian'] == 1:
      return 'Haw/Pac Isl.'
   if row['eri_white'] == 1:
      return 'White'
   return 'Other'

df.apply(lambda row: label_race(row), axis=1)
Comment

PREVIOUS NEXT
Code Example
Python :: list in python 
Python :: pandas in python 
Python :: Math Module exp() Function in python 
Python :: python print empty line 
Python :: PHP echo multiple lines example Using Nowdoc 
Python :: python count of values in array 
Python :: use functions to resample python 
Python :: python save picture in folder 
Python :: how to remove last 2 rows in a dataframe 
Python :: run python script without .py 
Python :: demonstrating polymorphism in python class 
Python :: Count the number of cells that contain a specific value in a pandas dataframe python 
Python :: Python How to convert a string to the name of a function? 
Python :: python check date between two dates 
Python :: what is attribute in python 
Python :: journalctl not showing all python prints 
Python :: tkinter how to update optionmenu contents 
Python :: how to add numbers into a list python 
Python :: how to open a file in python 
Python :: insta bs2json 
Python :: python parallelize for loop progressbar 
Python :: pandas join two dataframes 
Python :: python ip address increment 
Python :: simple keras model with one layer 
Python :: selenium proxy with authentication 
Python :: find rules of decision tree python 
Python :: cmap perlin noise python 
Python :: how to make a histogram with plotly for a single variable 
Python :: maya python override color rgb 
Python :: error:pip.subprocessor:command errored out with exit status 1: 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =