简体   繁体   English

Python:如何使用 2 个数据帧创建新的数据帧,这两个数据帧之一的数据必须被其他列名替换

[英]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循环,但是完成整个任务太慢了。)

df

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)

OUTPUT OUTPUT 在此处输入图像描述

暂无
暂无

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

相关问题 比较具有不同列名的两个数据框,并使用来自第二个数据框的列更新第一个数据框 - Compare two data-frames with different column names and update first data-frame with the column from second data-frame 汇总来自不同数据框的数据框列 - Summing data-frame columns from different data-frames Python Pandas:比较一列中的两个数据帧,并返回另一个数据帧中两个数据帧的行内容 - Python Pandas : compare two data-frames along one column and return content of rows of both data frames in another data frame 在python中匹配两个pandas数据帧的列名 - Matching the column names of two pandas data-frames in python Python,如何通过查找或.map 交互两个不同的数据帧 - Python, how to interact two different data-frames by lookup or .map 将多个未对齐的数据帧合并为单个pandas数据帧 - Merging multiple, unaligned data-frames into single pandas data-frame 匹配两个数据帧中的姓名和姓氏,从一个数据帧中提取中间名,并将 append 提取到另一个数据帧 - match name and surname from two data frames, extract middle name from one data frame and append it to other 根据数据框中另一列的值创建新列 - Create new column based on a value of another column in a data-frame 在Pandas Data-frame中使用掩码的字符串值创建一个新列 - Create a new column in Pandas Data-frame with a string value of a mask 在公共列中添加具有不同值的两个数据框 - Adding two data-frames with different values in a common column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM