Meta Interview Question

A table has a column with full names(e.g. 'Tom Hardin'). write a python code to get distinct first names(i know its super easy in sql)

Interview Answers

Anonymous

Mar 23, 2018

select distinct (substr(fullname, 0, instr(fullname,' ')-1)) from data

2

Anonymous

Apr 25, 2018

import pandas as pd df = pd.read_csv('name.csv') df['First Name'] = df['Full Name'].apply(lambda x: x.split(' ')[0]) df['Surname'] = df['Full Name'].apply(lambda x: x.split(' ')[1])

1

Anonymous

Sep 3, 2018

"write a python code to get distinct first names": # import libraries import pandas as pd import numpy as np # read the dataframe (requires sql set up - exercise for the reader) df = pd.read_sql('SELECT * FROM employees', con=mydb) # parse the full name into first name and surname (from previous contributor) df['First Name'] = df['fullname'].apply(lambda x: x.split(' ')[0]) df['Surname'] = df['fullname'].apply(lambda x: x.split(' ')[1]) # load the first name df in to a numpy array to use built in np 'unique' function x = np.array(df['First Name']) # print the unique names using the np.unique function print(np.unique(x))

Anonymous

Oct 8, 2018

assume you load everything already df['first_name'] = df['name'].split(' ')[0] df['first_name'].unique() it will return all the first name from the 'name' columns

Anonymous

Mar 31, 2018

Assuming table has only one column and first & last name separated with space. f = open('file','r') set([i.split(" ")[0] for i in f]) f.close