[英]group two dataframes with different sizes in python pandas
I've got two data frames, one has historical prices of stocks in this format:我有两个数据框,一个具有这种格式的股票历史价格:
year年 | Company1公司1 | Company2公司2 |
---|---|---|
1980 1980 | 4.66 4.66 | 12.32 12.32 |
1981 1981年 | 5.68 5.68 | 15.53 15.53 |
etc with hundreds of columns, then I have a dataframe specifing a company, its sector and its country.等有数百列,然后我有一个数据框指定一家公司,它的部门和它的国家。
company 1公司 1 | industrials工业 | Germany德国 |
---|---|---|
company 2公司 2 | consumer goods消费品 | US我们 |
company 3公司 3 | industrials工业 | France法国 |
I used the first dataframe to plot the prices of various companies over time, however, I'd like to now somehow group the data from the first table with the second one and create a separate dataframe which will have form of sectors total value of time, ie.我使用第一个数据框来绘制不同公司随时间变化的价格,但是,我现在想以某种方式将第一个表中的数据与第二个表进行分组,并创建一个单独的数据框,该数据框将具有行业总时间价值的形式, IE。
year年 | industrials工业 | consumer goods消费品 | healthcare卫生保健 |
---|---|---|---|
1980 1980 | 50.65 50.65 | 42.23 42.23 | 25.65 25.65 |
1981 1981年 | 55.65 55.65 | 43.23 43.23 | 26.15 26.15 |
Thank you谢谢
You can do the following, assuming df_1 is your DataFrame with price of stock per year and company, and df_2 your DataFrame with information on the companies:您可以执行以下操作,假设 df_1 是您的 DataFrame,其中包含每年和公司的股票价格,而 df_2 是您的 DataFrame,其中包含有关公司的信息:
# turn company columns into rows
df_1 = df_1.melt(id_vars='year', var_name='company')
df_1 = df_1.merge(df_2)
# groupby and move industry to columns
output = df_1.groupby(['year', 'industry'])['value'].sum().unstack('industry')
Output:输出:
industry consumer goods industrials
year
1980 12.32 4.66
1981 15.53 5.68
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.