Detailed explanation of pandas merge method

merge parameters

merge(
    left,
    right,
    how="inner",
    on=None,
    left_on=None,
    right_on=None,
    left_index=False,
    right_index=False,
    sort=False,
    suffixes=("_x", "_y"),
    copy=True,
    indicator=False,
    validate=None,
)
parameter Description
left Left table
right Right table
how Connection mode, inner, left, right, outer, the default is inner
on Column name used for connection
left_on The name of the column used to join the left table
right_on The name of the column used to join the right table
left_index Whether to use the row index of the left table as the connection key, the default is False
right_index Whether to use the row index of the right table as the connection key, the default is False
sort The default is False, sort the merged data
copy The default is True, always copy data to the data structure, set to False can improve performance
suffixes The suffix added after the column name when the same column name exists, the default is ('_x','_y')
indicator Show which table the data in the combined data comes from

left_on and right_on are mainly used when the column names of the two connected tables are different

DataFrame has an instance method join, which is equivalent to the parameters left_index=True and right_index=True of the merge method

inner、left、right、outer

Connection method

concat

concat can splice multiple DataFrames into one DataFrame

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0, 20, (5, 2)), columns=['A', 'B'])
print(df)

data = [df[0:2], df[3:]]
print(pd.concat(data))

append

Append is used to append rows, but concat is a static function of pd. Append is a method of DataFrame.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0, 20, (3, 2)), columns=['A', 'B'])
print(df)

narry = np.random.randint(0, 20, (2, 2))
data = pd.DataFrame(narry, columns=['A', 'B'])
print(df.append(data, ignore_index=True))

Guess you like

Origin blog.csdn.net/trayvontang/article/details/103787648