简体   繁体   English

基于两个参数创建新的 Pandas DataFrame

[英]Creating new Pandas DataFrame based on two parameters

This is my very first time asking a question:)这是我第一次提问:)

I have on Dataframe with company code (GVKEY) and for each company a set of dates with corresponding credit rating.我在带有公司代码 (GVKEY) 的 Dataframe 上,为每家公司提供了一组具有相应信用评级的日期。 I have another Dataframe with GVKEYs and a set of dates with corresponding financial ratios.我有另一个带有 GVKEY 的数据框和一组具有相应财务比率的日期。 I would like to create a new Dataframe where the companies, the dates and the ratio match.我想创建一个新的 Dataframe,其中公司、日期和比率匹配。

For now I have tried a for loop, looping only trough the same company in order to avoid looping over the whole dataset.现在我已经尝试了一个 for 循环,只通过同一家公司循环,以避免循环整个数据集。 Would their be a faster way of doing it?他们会是一种更快的方式吗?

DF with credit ratings具有信用评级的 DF

DF with financial ratios具有财务比率的 DF

DF with financial ratios matched with credit ratings, GVKEY and dates财务比率与信用评级、GVKEY 和日期相匹配的 DF

Try to use pandas.merge like this.尝试像这样使用 pandas.merge 。

pd.merge(df1, df2, left_on = ['company code', 'date'], right_on = ['company code', 'date']) pd.merge(df1, df2, left_on = ['公司代码', '日期'], right_on = ['公司代码', '日期'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM