Python 根据另一个数据框中的列值匹配列名
Python match a column name based on a column value in another dataframe
抱歉,如果这是某种重复,我查看了 20 个不同的问题,但其中 none 个对我有帮助。如果有人能给我指出一个问题来回答这个问题,我会很乐意删除我的问题。
我有两个数据框,第一个叫做 df_full 长长的各种列列表,其中一个叫做 'Industry' 并且有各种行业的字符串。 df_full['Industry'].head()
是:
INDEX
Industry
0
Service
1
Service
2
Trade
3
Service
4
Manufacturing
我的第二个数据框叫做 df_industry,它有基于每个行业的分位数。 df_industry['profit_sales']
是:
Industry
Financial
0.25
0.025616
0.50
0.219343
0.75
0.410408
Manufacturing
0.25
-0.012373
0.50
0.002032
0.75
0.010331
Service
0.25
-0.012660
0.50
0.003375
0.75
0.064102
Trade
0.25
-0.102178
0.50
0.001715
0.75
0.018705
Transport
0.25
-0.042755
0.50
-0.042755
0.75
0.056487
我正在尝试根据列行业中的行业为我的第一个数据框创建一个分位数为 0.5 的新列。
因此我的新输出 table 应该看起来像 df_full[['Industry','quantile_05']].head()
INDEX
Industry
quantile_05
0
Service
0.003375
1
Service
0.003375
2
Trade
0.001715
3
Service
0.003375
4
Manufacturing
0.002032
我目前试过无果:
df_full['quantile_05'] = df_full.apply(lambda x: df_industry['profit_sales'][df_full['Industry'][x]][0.5] ,axis=1)
看来你可以做一个地图:
df_full['quantile_05'] = df_full['Industry'].map(df_industry['profit_sales'].unstack()[0.5])
输出:
Industry quantile_05
INDEX
0 Service 0.003375
1 Service 0.003375
2 Trade 0.001715
3 Service 0.003375
4 Manufacturing 0.002032
如果您想要所有三个分位数,您可以按照 Kyle 的建议执行 merge
:
df_full.merge(df_industry['profit_sales'].unstack(),
left_on=['Industry'],
right_index=True,
how='left')
输出:
Industry 0.25 0.5 0.75
INDEX
0 Service -0.012660 0.003375 0.064102
1 Service -0.012660 0.003375 0.064102
2 Trade NaN 0.001715 0.018705
3 Service -0.012660 0.003375 0.064102
4 Manufacturing -0.012373 0.002032 0.010331
抱歉,如果这是某种重复,我查看了 20 个不同的问题,但其中 none 个对我有帮助。如果有人能给我指出一个问题来回答这个问题,我会很乐意删除我的问题。
我有两个数据框,第一个叫做 df_full 长长的各种列列表,其中一个叫做 'Industry' 并且有各种行业的字符串。 df_full['Industry'].head()
是:
INDEX | Industry |
---|---|
0 | Service |
1 | Service |
2 | Trade |
3 | Service |
4 | Manufacturing |
我的第二个数据框叫做 df_industry,它有基于每个行业的分位数。 df_industry['profit_sales']
是:
Industry | ||
---|---|---|
Financial | 0.25 | 0.025616 |
0.50 | 0.219343 | |
0.75 | 0.410408 | |
Manufacturing | 0.25 | -0.012373 |
0.50 | 0.002032 | |
0.75 | 0.010331 | |
Service | 0.25 | -0.012660 |
0.50 | 0.003375 | |
0.75 | 0.064102 | |
Trade | 0.25 | -0.102178 |
0.50 | 0.001715 | |
0.75 | 0.018705 | |
Transport | 0.25 | -0.042755 |
0.50 | -0.042755 | |
0.75 | 0.056487 |
我正在尝试根据列行业中的行业为我的第一个数据框创建一个分位数为 0.5 的新列。
因此我的新输出 table 应该看起来像 df_full[['Industry','quantile_05']].head()
INDEX | Industry | quantile_05 |
---|---|---|
0 | Service | 0.003375 |
1 | Service | 0.003375 |
2 | Trade | 0.001715 |
3 | Service | 0.003375 |
4 | Manufacturing | 0.002032 |
我目前试过无果:
df_full['quantile_05'] = df_full.apply(lambda x: df_industry['profit_sales'][df_full['Industry'][x]][0.5] ,axis=1)
看来你可以做一个地图:
df_full['quantile_05'] = df_full['Industry'].map(df_industry['profit_sales'].unstack()[0.5])
输出:
Industry quantile_05
INDEX
0 Service 0.003375
1 Service 0.003375
2 Trade 0.001715
3 Service 0.003375
4 Manufacturing 0.002032
如果您想要所有三个分位数,您可以按照 Kyle 的建议执行 merge
:
df_full.merge(df_industry['profit_sales'].unstack(),
left_on=['Industry'],
right_index=True,
how='left')
输出:
Industry 0.25 0.5 0.75
INDEX
0 Service -0.012660 0.003375 0.064102
1 Service -0.012660 0.003375 0.064102
2 Trade NaN 0.001715 0.018705
3 Service -0.012660 0.003375 0.064102
4 Manufacturing -0.012373 0.002032 0.010331