import pandas as pd

d = { '_key'        : pd.Series(['K0', 'K1','K2','K3']),
      '_key2'       : pd.Series(['z0', 'z1','z2','z3']),
      'hire_date'   : pd.Series(['h0', 'h1','h2','h3']),
      'professtion' : pd.Series(['p0', 'p1','p2','p3'])  }

d
Out[6]:
{'_key': 0    K0
 1    K1
 2    K2
 3    K3
 dtype: object, '_key2': 0    z0
 1    z1
 2    z2
 3    z3
 dtype: object, 'hire_date': 0    h0
 1    h1
 2    h2
 3    h3
 dtype: object, 'professtion': 0    p0
 1    p1
 2    p2
 3    p3
 dtype: object}
In [8]:
df_right = pd.DataFrame(d)
df_right
Out[8]:
_key_key2hire_dateprofesstion
0K0z0h0p0
1K1z1h1p1
2K2z2h2p2
3K3z3h3p3
In [9]:
d = { '_key'     : pd.Series(['K0', 'K1','K2','K3']),
      '_key2'    : pd.Series(['z0', 'z1','z2','z3']),
      'city'     : pd.Series(['c0', 'c1','c2','c3']),
      'user_name': pd.Series(['u0', 'u1','u2','u3'])  }
df_left = pd.DataFrame(d)
df_left
Out[9]:
_key_key2cityuser_name
0K0z0c0u0
1K1z1c1u1
2K2z2c2u2
3K3z3c3u3
In [11]:
pd.concat([df_left, df_left])  # merge rows of two dataframe
Out[11]:
_key_key2cityuser_name
0K0z0c0u0
1K1z1c1u1
2K2z2c2u2
3K3z3c3u3
0K0z0c0u0
1K1z1c1u1
2K2z2c2u2
3K3z3c3u3
In [12]:
pd.concat([df_left, df_right])
/home/hyeok/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:1: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.

To accept the future behavior, pass 'sort=False'.

To retain the current behavior and silence the warning, pass 'sort=True'.

  """Entry point for launching an IPython kernel.
Out[12]:
_key_key2cityhire_dateprofesstionuser_name
0K0z0c0NaNNaNu0
1K1z1c1NaNNaNu1
2K2z2c2NaNNaNu2
3K3z3c3NaNNaNu3
0K0z0NaNh0p0NaN
1K1z1NaNh1p1NaN
2K2z2NaNh2p2NaN
3K3z3NaNh3p3NaN
In [13]:
pd.concat([df_left, df_right], axis = 1, join = 'inner')
Out[13]:
_key_key2cityuser_name_key_key2hire_dateprofesstion
0K0z0c0u0K0z0h0p0
1K1z1c1u1K1z1h1p1
2K2z2c2u2K2z2h2p2
3K3z3c3u3K3z3h3p3
In [15]:
pd.merge(df_left, df_right, how = 'inner')
Out[15]:
_key_key2cityuser_namehire_dateprofesstion
0K0z0c0u0h0p0
1K1z1c1u1h1p1
2K2z2c2u2h2p2
3K3z3c3u3h3p3
In [16]:
df_left.append(df_right)
/home/hyeok/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py:6692: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.

To accept the future behavior, pass 'sort=False'.

To retain the current behavior and silence the warning, pass 'sort=True'.

  sort=sort)
Out[16]:
_key_key2cityhire_dateprofesstionuser_name
0K0z0c0NaNNaNu0
1K1z1c1NaNNaNu1
2K2z2c2NaNNaNu2
3K3z3c3NaNNaNu3
0K0z0NaNh0p0NaN
1K1z1NaNh1p1NaN
2K2z2NaNh2p2NaN
3K3z3NaNh3p3NaN
In [18]:
tags   = pd.read_csv("./ml/tags.csv"  )
movies = pd.read_csv("./ml/movies.csv")
In [20]:
tags.head()
Out[20]:
userIdmovieIdtagtimestamp
0260756funny1445714994
1260756Highly quotable1445714996
2260756will ferrell1445714992
3289774Boxing story1445715207
4289774MMA1445715200
In [21]:
movies.head()
Out[21]:
movieIdtitlegenres
01Toy Story (1995)Adventure|Animation|Children|Comedy|Fantasy
12Jumanji (1995)Adventure|Children|Fantasy
23Grumpier Old Men (1995)Comedy|Romance
34Waiting to Exhale (1995)Comedy|Drama|Romance
45Father of the Bride Part II (1995)Comedy
In [22]:
t = movies.merge(tags, on = "movieId", how = 'inner')
t.head()
Out[22]:
movieIdtitlegenresuserIdtagtimestamp
01Toy Story (1995)Adventure|Animation|Children|Comedy|Fantasy336pixar1139045764
11Toy Story (1995)Adventure|Animation|Children|Comedy|Fantasy474pixar1137206825
21Toy Story (1995)Adventure|Animation|Children|Comedy|Fantasy567fun1525286013
32Jumanji (1995)Adventure|Children|Fantasy62fantasy1528843929
42Jumanji (1995)Adventure|Children|Fantasy62magic board game1528843932
In [23]:
del t['timestamp']
In [24]:
t.head()
Out[24]:
movieIdtitlegenresuserIdtag
01Toy Story (1995)Adventure|Animation|Children|Comedy|Fantasy336pixar
11Toy Story (1995)Adventure|Animation|Children|Comedy|Fantasy474pixar
21Toy Story (1995)Adventure|Animation|Children|Comedy|Fantasy567fun
32Jumanji (1995)Adventure|Children|Fantasy62fantasy
42Jumanji (1995)Adventure|Children|Fantasy62magic board game
In [ ]:


+ Recent posts