xxxxxxxxxx
In [1]: s1 = pd.Series([1, 2], index=['A', 'B'], name='s1')
In [2]: s2 = pd.Series([3, 4], index=['A', 'B'], name='s2')
In [3]: pd.concat([s1, s2], axis=1)
Out[3]:
s1 s2
A 1 3
B 2 4
In [4]: pd.concat([s1, s2], axis=1).reset_index()
Out[4]:
index s1 s2
0 A 1 3
1 B 2 4
xxxxxxxxxx
# Horizontal concatenation = concat side by side, increase no of columns
horizontal_concat_df = pd.concat([df1,df2,df3], axis=1)
# Vertical concatenation = concat on top of another, increase no of rows
vertical_concat_df = pd.concat([df1,df2,df3], axis=0,
verify_integrity=False, # Verifies duplicate entries
ignore_index=False, # Retain index of source dfs
keys=['df1','df2','df3']) # Provide key for each source df
# Vertical concatenation : another approach
df = pd.DataFrame()
df = df.append(df_new)
xxxxxxxxxx
# Stack the DataFrames on top of each other
#survey_sub and survey_sub_last10 are both dataframes
vertical_stack = pd.concat([survey_sub, survey_sub_last10], axis=0)
# Place the DataFrames side by side
horizontal_stack = pd.concat([survey_sub, survey_sub_last10], axis=1)
xxxxxxxxxx
>>> pd.concat([students, pd.DataFrame(marks)], axis=1)
0 0
0 Alex 0.80
1 Lauren 0.75