[英]Python: How To create new data-frame with 2 data-frames that datas from one of those two have to be replaced by other's column name
All I want to do is to create a stocks' decile frame by industry factor model.我要做的就是通过行业因素 model 创建一个股票的十分位框架。
So far, I've made a decile frame based on industry's factor named as zongmok到目前为止,我已经制作了一个基于行业因素的十分位框架,名为 zongmok
And a frame called inds that shows information about which industry does stock belong to.还有一个名为 inds 的框架,显示有关股票属于哪个行业的信息。
The problem is that how to create a new data frame to show each stock's decile based on industry factor at every index time.问题是如何创建一个新的数据框来显示每个股票在每个指数时间基于行业因素的十分位数。 (The new data frame is called df, and I've tried to use squared for loop, But It was too slow to complete the whole task.)
(新的数据框叫df,我试过用平方for循环,但是完成整个任务太慢了。)
Here's the code I've tried:这是我尝试过的代码:
for i in notebook.tqdm(range(len(df))):
for j in notebook.tqdm(range(len(df.columns))):
try:
df[df.columns[j]].loc[df.index[i]] = zongmok[inds.loc[df.index][inds == zongmok.columns[j]].dropna(axis=1)[inds[inds == zongmok.columns[j]].dropna(axis=1).columns[j]].loc[df.index[i]]].loc[df.index[i]]
except Exception:
pass
else:
pass
By using merge()
it will be easier for you to join
two dataframe together without all of these iterations;通过使用
merge()
,您可以更轻松地join
两个 dataframe 连接在一起,而无需所有这些迭代;
Example:例子:
I've dataframe called prod_df
and I want to merge it with another dataframe called market_df
.我有 dataframe 称为
prod_df
,我想将它与另一个 dataframe 称为market_df
合并。 This's how it goes:事情是这样的:
Dataframe of prod_df
: prod_df 的
prod_df
:
Dataframe of market_df
: market_df 的
market_df
:
**Code Syntax for merging them using Inner Join
: **使用
Inner Join
合并它们的代码语法:
# Lets join the two dataframe depending on `prod_df
second_merge_df = pd.merge(prod_df, market_df, how='inner', on='Prod_id')
second_merge_df.sort_values(by='Prod_id', ascending=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.