python create new pandas dataframe with specific columns
# Basic syntax:
new_dataframe = old_dataframe.filter(['Columns','you','want'], axis=1)
python create new pandas dataframe with specific columns
# Basic syntax:
new_dataframe = old_dataframe.filter(['Columns','you','want'], axis=1)
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')
create new dataframe with columns from another dataframe pandas
new = old[['A', 'C', 'D']].copy()
select columns to include in new dataframe in python
new = old.filter(['A','B','D'], axis=1)
copy only some columns to new dataframe in r
new = pd.DataFrame([old.A, old.B, old.C]).transpose()
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)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us