Skip to content

Instantly share code, notes, and snippets.

@ahmed4end
Created July 7, 2021 11:39
Show Gist options
  • Save ahmed4end/341496e2e52743d75ae3112b2021a41f to your computer and use it in GitHub Desktop.
Save ahmed4end/341496e2e52743d75ae3112b2021a41f to your computer and use it in GitHub Desktop.
extract column value based on another column pandas dataframe
import pandas as pd
file1 = pd.read_csv('./n/oregonhie_descriptive_vars.csv')
file2 = pd.read_csv('./n/oregonhie_inperson_vars.csv')
file2['treatment'] = file2['person_id']
def proccess(key):
a = file1.loc[file1['person_id'] == key, 'treatment'].iloc[0]
return a
file2['treatment']= file2['treatment'].apply(proccess)
file2.to_csv('output.csv', encoding='utf-8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment