Python Pandas concat 的使用

2021-03-03 12:32:28 浏览数 (2)

1. axis(合并方向)


代码语言:javascript复制
import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.ones((3, 4)) * 0, columns = ['a', 'b', 'c', 'd'])
df2 = pd.DataFrame(np.ones((3, 4)) * 1, columns = ['a', 'b', 'c', 'd'])
df3 = pd.DataFrame(np.ones((3, 4)) * 2, columns = ['a', 'b', 'c', 'd'])

# concat
# 合并df1、df2、df3
# axis=0 纵向合并,axis=1 横向合并
# ignore_index=True 表示忽略以前的index,index重新生成
res =pd.concat([df1, df2, df3], axis = 0, ignore_index = True)
print(res)

2. join, ['inner', 'outer'] (合并方式)


代码语言:javascript复制
import pandas as pd
import numpy as np

# join, ['inner', 'outer']
df1 = pd.DataFrame(np.ones((3, 4)) * 0, columns = ['a', 'b', 'c', 'd'], index = [1, 2, 3])
df2 = pd.DataFrame(np.ones((3, 4)) * 1, columns = ['b', 'c', 'd', 'e'], index = [1, 2, 3])
print(df1)
print(df2)

# join默认outer模式,会将没有数据的位置使用NaN填充,类似于字段并集
res = pd.concat([df1, df2], join = 'outer')
print(res)
# join='inner',会将相同的部分进行合并,不同的部分被抛弃掉,类似于字段交集
res2 = pd.concat([df1, df2], join = 'inner', ignore_index = True)
print(res2)

3. join_axes(依照 axes 合并)


代码语言:javascript复制
import pandas as pd
import numpy as np

# join_axes
df1 = pd.DataFrame(np.ones((3, 4)) * 0, columns = ['a', 'b', 'c', 'd'], index = [1, 2, 3])
df2 = pd.DataFrame(np.ones((3, 4)) * 1, columns = ['b', 'c', 'd', 'e'], index = [2, 3, 4])
# 以df1的index为准,df2没有的index填充NaN,df2有但df1没有的index直接抛弃
res = pd.concat([df1, df2], axis = 1, join_axes = [df1.index])
print(res)

4. append(添加数据)


代码语言:javascript复制
import pandas as pd
import numpy as np

# append
df1 = pd.DataFrame(np.ones((3, 4)) * 0, columns = ['a', 'b', 'c', 'd'])
df2 = pd.DataFrame(np.ones((3, 4)) * 1, columns = ['a', 'b', 'c', 'd'])
df3 = pd.DataFrame(np.ones((3, 4)) * 2, columns = ['a', 'b', 'c', 'd'])

# 追加记录,添加记录
res = df1.append([df2, df3], ignore_index = True)
print(res)

# 在df1后添加一条记录
s1 = pd.Series([1, 2, 3, 4], index = ['a', 'b', 'c', 'd'])
print(s1)
res = df1.append(s1, ignore_index = True)
print(res)

0 人点赞