create dataframe from another dataframe column code example
Example 1: select columns to include in new dataframe in python
new = old[['A', 'C', 'D']].copy()
Example 2: select columns to include in new dataframe in python
new = old.filter(['A','B','D'], axis=1)
Example 3: copy only some columns to new dataframe in r
new = pd.DataFrame([old.A, old.B, old.C]).transpose()
Example 4: 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)