[英]Python dates filling in blanks, pandas dataframe
I have a dataframe with about two years worth of dates. 我有一个大约两年的日期数据框。 I am trying to create a new column that will indicate if the date has earnings reported on that day. 我正在尝试创建一个新列,以指示该日期是否有当天的收入报告。 How can I do this? 我怎样才能做到这一点?
DF DF
Date Stock
2018-02-15 100
.
.
.
2018-04-02 122
2018-04-03 119
2018-04-04 120
My attempt I looked up the earning dates then I tired to join but failed. 我尝试查找收入日期,然后累了,但失败了。
df = pd.DataFrame(columns=["Earnings_Date", "Earnings Reported"],
data=[['05/01/2018','Yes'],['02/15/2018','Yes'],['10/31/2017','Yes'],['08/01/2017','Yes']])
Ideally, I would like my DF to look like 理想情况下,我希望我的DF看起来像
DF DF
Date Stock Earnings Reported
2018-02-15 100 Yes
.
.
.
2018-04-02 122 No
2018-04-03 119 No
2018-04-04 120 No
No need for join, merging. 无需加入,合并。 Just check to see if Date is in the Earnings_Date column. 只需检查日期是否在Earnings_Date列中即可。
df['Earnings Report'] = np.where(df['Date'].isin(mydf['Earnings_Date']),'Yes','No')
Where mydf is the Earnings Report dataframe, and df is the original stock dataframe. 其中,mydf是收益报告数据框,而df是原始股票数据框。
Date Stock Earnings Report
0 2018-02-15 100 Yes
1 2018-04-02 122 No
2 2018-04-03 119 No
3 2018-04-04 120 No
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.