python - Python Pandas 合并和更新数据框
问题描述
我目前正在使用 Python 和 Pandas 来形成股票价格“数据库”。我设法找到了一些代码来下载股票价格。
df1 是我现有的数据库。每次我下载股价时,它都会看起来像 df2 和 df3。然后,我需要将 df1、df2 和 df3 数据组合起来看起来像 df4。
每只股票都有自己的专栏。每个日期都有自己的行。
df1:现有数据库
+----------+-------+----------+--------+
| Date | Apple | Facebook | Google |
+----------+-------+----------+--------+
| 1/1/2018 | 161 | 58 | 1000 |
| 2/1/2018 | 170 | 80 | |
| 3/1/2018 | 190 | 84 | 100 |
+----------+-------+----------+--------+
df2:Google 的新数据(2/1/2018 和 4/1/2018)和更新数据(3/1/2018)。
+----------+--------+
| Date | Google |
+----------+--------+
| 2/1/2018 | 500 |
| 3/1/2018 | 300 |
| 4/1/2018 | 200 |
+----------+--------+
df3:亚马逊的新数据
+----------+--------+
| Date | Amazon |
+----------+--------+
| 1/1/2018 | 1000 |
| 2/1/2018 | 1500 |
| 3/1/2018 | 2000 |
| 4/1/2018 | 3000 |
+----------+--------+
df4 最终输出:基本上,它将所有数据合并并更新到数据库中。(df1 + df2 + df3) --> 这将是 df1 的更新数据库
+----------+-------+----------+--------+--------+
| Date | Apple | Facebook | Google | Amazon |
+----------+-------+----------+--------+--------+
| 1/1/2018 | 161 | 58 | 1000 | 1000 |
| 2/1/2018 | 170 | 80 | 500 | 1500 |
| 3/1/2018 | 190 | 84 | 300 | 2000 |
| 4/1/2018 | | | 200 | 3000 |
+----------+-------+----------+--------+--------+
我不知道如何结合df1
和df3
。
而且我不知道如何合并df1
和df2
(添加新行:2018 年 4 月 1 日),同时更新数据(2018 年 2 月 1 日-> 原始数据:NaN;修改后的数据:500 | 3/1/ 2018 -> 原始数据:100;修正数据:300)并保留现有完整数据(2018 年 1 月 1 日)。
任何人都可以帮我获得 df4 吗?=)
谢谢你。
编辑:根据 Sociopath 的建议,我将代码修改为:
dataframes = [df2, df3]
df4 = df1
for i in dataframes:
# Merge the dataframe
df4 = df4.merge(i, how='outer', on='date')
# Get the stock name
stock_name = i.columns[1]
# To check if there is any column with "_x", if have, then combine these columns
if stock_name+"_x" in df4.columns:
x = stock_name+"_x"
y = stock_name+"_y"
df4[stock_name] = df4[y].fillna(df4[x])
df4.drop([x, y], 1, inplace=True)
解决方案
你需要merge
:
df1 = pd.DataFrame({'date':['2/1/2018','3/1/2018','4/1/2018'], 'Google':[500,300,200]})
df2 = pd.DataFrame({'date':['1/1/2018','2/1/2018','3/1/2018','4/1/2018'], 'Amazon':[1000,1500,2000,3000]})
df3 = pd.DataFrame({'date':['1/1/2018','2/1/2018','3/1/2018'], 'Apple':[161,171,181], 'Google':[1000,None,100], 'Facebook':[58,75,65]})
如果当前数据库中不存在该列,只需使用merge
如下
df_new = df3.merge(df2, how='outer',on=['date'])
如果数据库中存在该列,则使用fillna
如下更新值:
df_new = df_new.merge(df1, how='outer', on='date')
#print(df_new)
df_new['Google'] = df_new['Google_y'].fillna(df_new['Google_x'])
df_new.drop(['Google_x','Google_y'], 1, inplace=True)
输出:
date Apple Facebook Amazon Google
0 1/1/2018 161.0 58.0 1000 1000.0
1 2/1/2018 171.0 75.0 1500 500.0
2 3/1/2018 181.0 65.0 2000 300.0
3 4/1/2018 NaN NaN 3000 200.0
编辑
后面部分更通用的解决方案。
dataframes = [df2, df3, df4]
for i in dataframes:
stock_name = list(i.columns.difference(['date']))[0]
df_new = df_new.merge(i, how='outer', on='date')
x = stock_name+"_x"
y = stock_name+"_y"
df_new[stock_name] = df_new[y].fillna(df_new[x])
df_new.drop([x,y], 1, inplace=True)
推荐阅读
- java - Andorid Java中通过SSH隧道的简单袜子
- javascript - 我放在折线图选项中的所有内容都不起作用
- xamarin - Xamarin 将基本 BindingContext 形成为 ViewModel
- google-chrome-devtools - Lighthouse 开发工具 - 具有身份验证的站点
- python - Pool.map 循环和内存错误
- selenium - 尝试使用硒单击横幅
- mysql - 问题在使用 express 节点的虚拟机中加入数据库 mysql
- oracle - 无法从 JDBC 获取 OUT 参数中的二维关联数组
- ios - 键盘导致 UICollectionView Cells 加载错误
- maven - Maven故障安全插件不执行测试