Vous êtes sur la page 1sur 3

Pandas concat and join

October 19, 2016

0.1 We will learn how to concatane and join one or more dataframe
In [1]: import pandas as pd
froam IPython.display import display
from IPython.display import Image

In [27]: raw_data = {
'subject_id': ['1', '2', '3', '4', '5'],
'first_name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'],
'last_name': ['Anderson', 'Ackerman', 'Ali', 'Aoni', 'Atiches']}
df_a = pd.DataFrame(raw_data, columns = ['subject_id', 'first_name', 'last
df_a

Out[27]: subject_id first_name last_name


0 1 Alex Anderson
1 2 Amy Ackerman
2 3 Allen Ali
3 4 Alice Aoni
4 5 Ayoung Atiches

In [21]: raw_data = {
#'subject_id': ['4', '5', '6', '7', '8'],
#'subject_id': ['1', '2', '3', '4', '8'],
#'first_name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
#'last_name': ['Bonder', 'Black', 'Balwner', 'Brice', 'Btisan']}
'subject_id': ['1', '2', '3', '4'],
'first_name': ['Billy', 'Brian', 'Bran', 'Bryce'],
'last_name': ['Bonder', 'Black', 'Balwner', 'Brice',]}
df_b = pd.DataFrame(raw_data, columns = ['subject_id', 'first_name', 'last
df_b

Out[21]: subject_id first_name last_name


0 1 Billy Bonder
1 2 Brian Black
2 3 Bran Balwner
3 4 Bryce Brice

In [25]: try:
df = df_b[df_a['subject_id'] == df_b['subject_id']]

1
except (ValueError, IndexError):
pass
df
Out[25]: subject_id first_name last_name
0 1 Alex Anderson
1 2 Amy Ackerman
2 3 Allen Ali
3 4 Alice Aoni
In [19]: df1 = df_b[df_a['subject_id'] == df_b['subject_id']]
df1
Out[19]: subject_id first_name last_name
0 1 Billy Bonder
1 2 Brian Black
2 3 Bran Balwner
3 4 Bryce Brice
In [20]: try:
# Match source and destination to get coordinates.
source1 = df_a[df_a["subject_id"] == df_b["subject_id"]]
dest1 = df_b[df_a["subject_id"] == df_b["subject_id"]]
# Use coordinates to compute distance.
except (ValueError, IndexError):
pass
print source1
print
print dest1
subject_id first_name last_name
0 1 Alex Anderson
1 2 Amy Ackerman
2 3 Allen Ali
3 4 Alice Aoni

subject_id first_name last_name


0 1 Billy Bonder
1 2 Brian Black
2 3 Bran Balwner
3 4 Bryce Brice

In [15]: print pd.concat([source,dest], axis=1)


subject_id first_name last_name subject_id first_name last_name
0 1 Alex Anderson 1 Billy Bonder
1 2 Amy Ackerman 2 Brian Black
2 3 Allen Ali 3 Bran Balwner
3 4 Alice Aoni 4 Bryce Brice

2
In [ ]: