楼主: knell
1393 11

[问答] python 将两组时间序列中不同数据 按照日期 降序上下合并连接 ? [推广有奖]

  • 2关注
  • 0粉丝

已卖:49份资源

博士生

49%

还不是VIP/贵宾

-

威望
0
论坛币
483 个
通用积分
50.7675
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
7294 点
帖子
227
精华
0
在线时间
241 小时
注册时间
2010-2-22
最后登录
2025-6-27

楼主
knell 发表于 2023-6-24 11:05:27 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

import pandas as pd
print("数据1")
grades_df1 = pd.DataFrame(data=
{'日期': ["2022-01-01", "2022-01-02", "2022-01-03", "2022-01-04", "2022-01-05", "2022-01-06", "2022-01-07", "2022-01-08", "2022-01-10","2022-01-11"],
'exam1': [24, 63, 56, 56, 67, 51, 79, 46, 72, 60]})
print(grades_df1)
print(type(grades_df1))
"""
数据1
日期 exam1
0 2022-01-01 24
1 2022-01-02 63
2 2022-01-03 56
3 2022-01-04 56
4 2022-01-05 67
5 2022-01-06 51
6 2022-01-07 79
7 2022-01-08 46
8 2022-01-10 72
9 2022-01-11 60
<class 'pandas.core.frame.DataFrame'>
"""

print("数据2")
grades_df2 = pd.DataFrame(data=
{'日期': ["2022-01-01", "2022-01-02", "2022-01-03", "2022-01-04", "2022-01-05", "2022-01-06", "2022-01-07", "2022-01-08", "2022-01-09","2022-10-01"],
'exam2': [24, 63, 56, 56, 67, 51, 79, 46, 7, 60]})
print(grades_df2)
print(type(grades_df2))
print("数据2")
"""
数据2
日期 exam2
0 2022-01-01 24
1 2022-01-02 63
2 2022-01-03 56
3 2022-01-04 56
4 2022-01-05 67
5 2022-01-06 51
6 2022-01-07 79
7 2022-01-08 46
8 2022-01-09 7
9 2022-10-01 60
<class 'pandas.core.frame.DataFrame'>
"""

# 希望的结果
"""
日期 exam1
0 2022-01-01 24
1 2022-01-02 63
2 2022-01-03 56
3 2022-01-04 56
4 2022-01-05 67
5 2022-01-06 51
6 2022-01-07 79
7 2022-01-08 46
8 2022-10-01 60
9 2022-01-10 72
10 2022-10-11 60
<class 'pandas.core.frame.DataFrame'>
"""

'''

实际数据为多个数据集设置使用动态变量两两合并, 每个数据集都含有多个列, 代码尽量不要太多,太繁琐.

'''

##谢谢


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python 时间序列 Dataframe import pandas
相关内容:Python数据合并

沙发
cjl2399 发表于 2023-6-27 09:46:28
这个真不知道,不好意思

藤椅
shadowaver 在职认证  发表于 2023-6-27 10:07:25
pd.concat()或者pd.append()方法试试

板凳
jnutt 学生认证  发表于 2023-6-28 09:40:08
grades_df2.rename(columns={'exam2':'exam1'},inplace=True)
temp = grades_df1.append(grades_df2)
temp.sort_values(by='日期').drop_duplicates().reset_index(drop=True)

报纸
Studio-R 在职认证  发表于 2023-6-28 18:45:33
先合并后排序
  1. pd.concat([grades_df1,grades_df1]).sort_values('日期',ascending = False)
复制代码

地板
Studio-R 在职认证  发表于 2023-6-28 18:46:37
顺一提,论坛的帖子编辑器是有些代码的功能的,直接贴文本看起来比较乱

7
knell 发表于 2024-3-20 11:44:23
Studio-R 发表于 2023-6-28 18:46
顺一提,论坛的帖子编辑器是有些代码的功能的,直接贴文本看起来比较乱

8
knell 发表于 2024-3-20 11:51:02
Studio-R 发表于 2023-6-28 18:45
先合并后排序
谢谢

9
knell 发表于 2024-3-20 11:51:54
# 整理好 代码如有需要直接拷贝

import pandas as pd
print("数据1")
grades_df1 = pd.DataFrame(data=
{'日期': ["2022-01-01", "2022-01-02", "2022-01-03", "2022-01-04", "2022-01-05", "2022-01-06", "2022-01-07", "2022-01-08", "2022-01-10","2022-01-11"],
'exam1': [24, 63, 56, 56, 67, 51, 79, 46, 72, 60]})
print(grades_df1)
print(type(grades_df1))
"""
数据1
           日期  exam1
0  2022-01-01     24
1  2022-01-02     63
2  2022-01-03     56
3  2022-01-04     56
4  2022-01-05     67
5  2022-01-06     51
6  2022-01-07     79
7  2022-01-08     46
8  2022-01-10     72
9  2022-01-11     60
<class 'pandas.core.frame.DataFrame'>
"""
import pandas as pd
print("数据2")
grades_df1 = pd.DataFrame(data=
{'日期': ["2022-01-01", "2022-01-02", "2022-01-03", "2022-01-04", "2022-01-05", "2022-01-06", "2022-01-07", "2022-01-08", "2022-01-10","2022-01-11"],
'exam1': [24, 63, 56, 56, 67, 51, 79, 46, 72, 60]})
print(grades_df1)
print(type(grades_df1))
"""
数据2
           日期  exam1
0  2022-01-01     24
1  2022-01-02     63
2  2022-01-03     56
3  2022-01-04     56
4  2022-01-05     67
5  2022-01-06     51
6  2022-01-07     79
7  2022-01-08     46
8  2022-01-10     72
9  2022-01-11     60
<class 'pandas.core.frame.DataFrame'>
"""
print("合并")
# 按日期顺序 降序排列 ascending = True(False) 升降序
df = pd.concat([grades_df1,grades_df1]).sort_values('日期',ascending = False)
print(df)
print(type(df))
"""
合并
           日期  exam1
9  2022-01-11     60
9  2022-01-11     60
8  2022-01-10     72
8  2022-01-10     72
7  2022-01-08     46
7  2022-01-08     46
6  2022-01-07     79
6  2022-01-07     79
5  2022-01-06     51
5  2022-01-06     51
4  2022-01-05     67
4  2022-01-05     67
3  2022-01-04     56
3  2022-01-04     56
2  2022-01-03     56
2  2022-01-03     56
1  2022-01-02     63
1  2022-01-02     63
0  2022-01-01     24
0  2022-01-01     24
<class 'pandas.core.frame.DataFrame'>
"""
# https://bbs.pinggu.org/thread-11525301-1-1.html
# Studio-R

10
knell 发表于 2024-3-20 11:55:13
jnutt 发表于 2023-6-28 09:40
grades_df2.rename(columns={'exam2':'exam1'},inplace=True)
temp = grades_df1.append(grades_df2)
te ...
谢谢

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-11 01:43