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 :: how to add row to the Dataframe in python 
Python :: python save figure as pdf 
Python :: python check if file has content 
Python :: images subplot python 
Python :: from csv to pandas dataframe 
Python :: how to read zip csv file in python 
Python :: find position of nan pandas 
Python :: djangodebug toolbar not showing 
Python :: iterative binary search python 
Python :: discord.py create text channel 
Python :: decode base64 python 
Python :: python how to get html code from url 
Python :: pandas read csv without index 
Python :: kmeans sklearn 
Python :: taking string input from user in python 
Python :: import tknter 
Python :: how to close python with a line of code 
Python :: python f string columns 
Python :: python trim string to length 
Python :: pandas get numeric columns 
Python :: sha256 pandas 
Python :: ellipsis in python as index 
Python :: th2=cv2.adaptiveThreshold(img, 255 ,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11 # no of block size , 2 #c) 
Python :: check value vowel user input python 
Python :: listing index elasticsearch python 
Python :: cartesian product of a list python 
Python :: sdsdsdsdsddsdddsdsdsdsdsdsdsdsdsdsdsdsdsdssdsdsdsdsdsdsdssssssddsdssdssssdsdsdsdsdsdsdsdsdsdsdsdsdsdssdssdsdsdsdsdsdsdsdsdsdsdssd 
Python :: how to print something in python 
Python :: python record screen 
Python :: python get date next week 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =