stack two dataframes pandas code example

Example 1: pandas left join

df.merge(df2, left_on = "doc_id", right_on = "doc_num", how = "left")

Example 2: append one row to pandas dataframe

df = df.append({'index1': value1, 'index2':value2,...}, ignore_index=True)

Example 3: merge two df

bigdata = pd.concat([data1, data2], ignore_index=True, sort=False)

Example 4: appending dataframes pandas

>>> df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
>>> df
   A  B
0  1  2
1  3  4
>>> df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
>>> df.append(df2)
   A  B
0  1  2
1  3  4
0  5  6
1  7  8

Example 5: dataframe concatenate

# Pandas for Python

df['col1 & col2'] = df['col1']+df['col2']

#Output
#col1	col2	col1 & col2
#A1		A2		A1A2
#B1		B2		B1B2

Example 6: concact geodataframe python

>>> s1 = pd.Series(['a', 'b'])
>>> s2 = pd.Series(['c', 'd'])
>>> pd.concat([s1, s2])
0    a
1    b
0    c
1    d
dtype: object

Tags:

R Example