python Dataframe 合并与去重

1.1 结构合并

将两个结构相同的数据合并

  • 函数配置
concat([dataFrame1, dataFrame2,...], index_ingore=False)

参数说明:index_ingore=False(表示合并的索引不延续),index_ingore=True(表示合并的索引可延续)

import pandas as pd
import numpy as np

创建一个十行两列的二维数据
df = pd.DataFrame(np.random.randint(0, 10, (3, 2)), columns=['A', 'B'])

将数据拆分成两份,并保存在列表中
data_list = [df[0:2], df[3:]]

索引值不延续
df1 = pd.concat(data_list, ignore_index=False)

索引值延续
df2 = pd.concat(data_list, ignore_index=True)
  • 返回结果
   A  B
0  7  8
1  7  3
3  4  0# -------------->这里并没有2出现,索引不连续
4  1  8
   A  B
0  5  6
1  1  2
2  5  3
3  1  8
4  1  2
  key  data1
0   a      0
1   b      1
2   c      2
  key  data1  data2
0   a      0      0
1   b      1      1
2   c      2      2
  • 实例2
使用多个密钥进行连接时,传入连接密钥列表<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Pass in the list of connection keys when you connect with multiple keys</font>*</details>

right=DataFrame({'key1':['foo','foo','bar','bar'],
         'key2':['one','one','one','two'],
         'lval':[4,5,6,7]})

left=DataFrame({'key1':['foo','foo','bar'],
         'key2':['one','two','one'],
         'lval':[1,2,3]})

pd.merge(left,right,on=['key1','key2'],how='outer')
  • 结果展示

`python
key1 key2 lval
0 foo one 1
1 foo two 2
2 bar one 3
brand style rating
0 Yum Yum cup 4.0
1 Yum Yum cup 4.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Indomie pack 5.0

Original: https://blog.51cto.com/coderusher/5554275
Author: Coderusher
Title: python Dataframe 合并与去重

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/504381/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球