Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas new column from others

import pandas as pd

#Here a and b are existing columns

df['c'] = df.apply(lambda row: row.a + row.b, axis=1)
df
#    a  b  c
# 0  1  3  4
# 1  2  4  6
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 get something from a certian possition in a list python 
Python :: plt change grid color 
Python :: Get List Into String 
Python :: create empty pandas dataframe 
Python :: save dictionary to file numpy 
Python :: python convert list of strings to list of integers 
Python :: with urllib.request.urlopen("https:// 
Python :: django redirect to external url 
Python :: pandas count freq of each value 
Python :: state_dict() 
Python :: lista to txt python 
Python :: how to create table in a database in python 
Python :: find the closest smaller value in an array python 
Python :: python convert list to absolute value 
Python :: python absolute value 
Python :: convert list to string 
Python :: adding columns in cpecific position 
Python :: python datetime get all days between two dates 
Python :: index of max in tensor 
Python :: how to set up dataframe from csv 
Python :: save and load a machine learning model using Pickle 
Python :: python selenium web scraping example 
Python :: networkx largest component 
Python :: python how to get the screen size 
Python :: convert string to list of dictionaries 
Python :: how to flatten a nested list in python 
Python :: pandas difference between dates 
Python :: check if anything in a list is in a string python 
Python :: how to redirect in django rest framework 
Python :: Changing the number of ticks on a Matplotlib plot axis 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =