
[英]Pandas: compare two separate dataframes, find intersection of one column, but use value in another column to determine which dataframe to keep?
[英]Compare two dataframes and find the rows that are not in the second dataframe by comparing by one column
我希望获取具有相同列的 2 个数据帧,并基于 1 列比较它们,并生成一个新的数据帧,这些行没有出现在第二个数据帧中。 这是带有打印类型的数据框的片段。 我也试过pd.to_datetime(df['Date'])
以确保我将苹果与苹果进行比较。
df1
Date object
Open float64
High float64
Low float64
Close float64
Volume int32
dtype: object
Date Open High Low Close Volume
0 2000-01-04 24.35 24.35 23.55 23.70 1372800
1 2000-01-05 23.80 23.90 23.38 23.38 2746800
2 2000-01-06 23.48 23.82 23.38 23.70 2243200
3 2000-01-07 23.60 23.60 23.12 23.45 1610400
4 2000-01-10 23.48 23.48 22.90 22.90 1728600
df2
Date object
Open object
High object
Low object
Close object
Volume int64
dtype: object
Date Open High Low Close Volume
0 2000-01-04 24.35 24.35 23.55 23.70 1372800
1 2000-01-05 23.80 23.90 23.38 23.38 2746800
2 2000-01-06 23.48 23.83 23.38 23.70 2243200
3 2000-01-07 23.60 23.60 23.13 23.45 1610400
输出
Date Open High Low Close Volume
4 2000-01-10 23.48 23.48 22.90 22.90 1728600
我使用的代码是:
df1[~df1.Date.isin(df2.Date.values)]
当我从 .csv 加载 df1 时,这会起作用,但是当我从网站上提取它时,它会生成所有行。 我不明白比较如何不将日期视为相同。
我已将它们保存为 .csv 并进行了比较,但我看不出有什么不同。
任何诊断此问题的帮助将不胜感激。
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.