Pandas DataFrame merge 数据合并
【摘要】 环境信息ModelArtsNotebook - Multi-Engine 2.0 (python3)JupyterLab - Notebook - Conda-python3pandas 0.22.0 Pandas DataFrame merge 数据合并import pandas as pd# 情景描述# df1是一个时序序列的数据集# pd.date_range(start='202...
环境信息
- ModelArts
- Notebook - Multi-Engine 2.0 (python3)
- JupyterLab - Notebook - Conda-python3
- pandas 0.22.0
- JupyterLab - Notebook - Conda-python3
- Notebook - Multi-Engine 2.0 (python3)
Pandas DataFrame merge 数据合并
import pandas as pd
# 情景描述
# df1是一个时序序列的数据集
# pd.date_range(start='2021-3-1',periods=7) 生成时间序列
df1 = pd.DataFrame({"SN":["A","B","C","A","A","B","D"],"value_1":[1,2,3,11,111,22,4],"time":pd.date_range(start='2021-3-1',periods=7)})
# df2是一个 计算后得出的结果
# SN 列中都是唯一的,每个SN都有唯一对应的值
df2 = pd.DataFrame({"SN":["A","B","C","E"],"max":[1000,2000,3000,4000]})
df1
df2
# 相同的列名SN进行连接,how='inner'
pd.merge(df1,df2)
# 右连接 df2是完整的
pd.merge(df1,df2,how="right")
# 左连接 df1是完整的
pd.merge(df1,df2,how="left")
# 取并集
pd.merge(df1,df2,how="outer")
help
help(pd.merge)
Help on function merge in module pandas.core.reshape.merge:
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)
Merge DataFrame objects by performing a database-style join operation by
columns or indexes.
If joining columns on columns, the DataFrame indexes *will be
ignored*. Otherwise if joining indexes on indexes or indexes on a column or
columns, the index will be passed on.
Parameters
----------
left : DataFrame
right : DataFrame
how : {'left', 'right', 'outer', 'inner'}, default 'inner'
* left: use only keys from left frame, similar to a SQL left outer join;
preserve key order
* right: use only keys from right frame, similar to a SQL right outer join;
preserve key order
* outer: use union of keys from both frames, similar to a SQL full outer
join; sort keys lexicographically
* inner: use intersection of keys from both frames, similar to a SQL inner
join; preserve the order of the left keys
......
备注
- 欢迎各位同学一起来交流学习心得^_^
- 在线课程、沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)