赞
踩
merge()
函数是 Pandas 库中一个非常强大的工具,用于根据一个或多个键将两个 DataFrame 合并起来。这是数据整合和清洗中经常使用的操作。merge()
函数提供了多种合并方式,包括内连接(inner join)、左连接(left join)、右连接(right join)和外连接(outer join)。df1
和 df2
,我们想要根据某个共同的列(例如 'key')来合并它们。
- import pandas as pd
-
- # 创建两个示例 DataFrame
- df1 = pd.DataFrame({
- 'key': ['A', 'B', 'C', 'D'],
- 'value1': [1, 2, 3, 4]
- })
- print(df1)
- """
- key value1
- 0 A 1
- 1 B 2
- 2 C 3
- 3 D 4
- """
-
- df2 = pd.DataFrame({
- 'key': ['B', 'C', 'D', 'E'],
- 'value2': [5, 6, 7, 8]
- })
- print(df2)
- """
- key value2
- 0 B 5
- 1 C 6
- 2 D 7
- 3 E 8
- """
-
- # 使用 merge() 函数合并
- merged = pd.merge(df1, df2, on='key')
- print(merged)
- """
- key value1 value2
- 0 B 2 5
- 1 C 3 6
- 2 D 4 7
- """
- import pandas as pd
-
- # 创建两个示例 DataFrame
- df1 = pd.DataFrame({
- 'key': ['A', 'B', 'C', 'D'],
- 'value1': [1, 2, 3, 4]
- })
- print(df1)
- """
- key value1
- 0 A 1
- 1 B 2
- 2 C 3
- 3 D 4
- """
-
- df2 = pd.DataFrame({
- 'key': ['B', 'C', 'D', 'E'],
- 'value2': [5, 6, 7, 8]
- })
- print(df2)
- """
- key value2
- 0 B 5
- 1 C 6
- 2 D 7
- 3 E 8
- """
-
- # 使用 merge() 函数合并
- merged = pd.merge(df1, df2, on='key')
- print(merged)
- """
- key value1 value2
- 0 B 2 5
- 1 C 3 6
- 2 D 4 7
- """
-
- # 内连接(inner join)
- inner_merged = pd.merge(df1, df2, on='key', how='inner')
- print("Inner Merge:\n", inner_merged)
- """
- Inner Merge:
- key value1 value2
- 0 B 2 5
- 1 C 3 6
- 2 D 4 7
- """
-
- # 左连接(left join)
- left_merged = pd.merge(df1, df2, on='key', how='left')
- print("\nLeft Merge:\n", left_merged)
- """
- Left Merge:
- key value1 value2
- 0 A 1 NaN
- 1 B 2 5.0
- 2 C 3 6.0
- 3 D 4 7.0
- """
-
- # 右连接(right join)
- right_merged = pd.merge(df1, df2, on='key', how='right')
- print("\nRight Merge:\n", right_merged)
- """
- Right Merge:
- key value1 value2
- 0 B 2.0 5
- 1 C 3.0 6
- 2 D 4.0 7
- 3 E NaN 8
- """
-
- # 外连接(outer join)
- outer_merged = pd.merge(df1, df2, on='key', how='outer')
- print("\nOuter Merge:\n", outer_merged)
- """
- Outer Merge:
- key value1 value2
- 0 A 1.0 NaN
- 1 B 2.0 5.0
- 2 C 3.0 6.0
- 3 D 4.0 7.0
- 4 E NaN 8.0
- """
-
- # 全连接 (cross)
- cross_merged = pd.merge(df1, df2, how='cross')
- print("\nCross Merge:\n", cross_merged)
- """
- Cross Merge:
- key_x value1 key_y value2
- 0 A 1 B 5
- 1 A 1 C 6
- 2 A 1 D 7
- 3 A 1 E 8
- 4 B 2 B 5
- 5 B 2 C 6
- 6 B 2 D 7
- 7 B 2 E 8
- 8 C 3 B 5
- 9 C 3 C 6
- 10 C 3 D 7
- 11 C 3 E 8
- 12 D 4 B 5
- 13 D 4 C 6
- 14 D 4 D 7
- 15 D 4 E 8
- """
merge()
函数是 Pandas 中非常强大的工具,它允许你根据一个或多个键灵活地合并 DataFrame。通过熟练掌握 merge()
函数,你可以更有效地整合和清洗数据。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。