pandas 如何将数据框与透视表连接

vi4fp9gy  于 2022-12-21  发布在  其他
关注(0)|答案(1)|浏览(91)

这是我的数据框
| 识别号|总计|部分和|
| - ------| - ------| - ------|
| A1|四十|二十五|
| A2|七十|五十|
| A3|一百|四十|
我想根据以下给定网格的透视表构建第三列,比如:
| 总和区间\部分区间和|0 - 30岁|三十至五十五岁|55至70岁|
| - ------| - ------| - ------| - ------|
| 0-50 |0.10分| 0.17|0.22|
| 50至75岁|0.14| 0.18| 0.25|
| 75 - 100人|0.20| 0.27| 0.38|
这将产生以下预期结果:
| 识别号|总计|部分和|电网给定比值|
| - ------| - ------| - ------| - ------|
| A1|四十|二十五|0.10分|
| A2|七十|五十|0.18|
| A3|一百|四十|0.27|
我想知道做这件事最方便的方法是什么?
谢谢你,

yzuktlbb

yzuktlbb1#

使用pd.IntervalIndexloc尝试以下操作:

import pandas as pd

df = pd.read_clipboard() # Copy input from question above
df1 = pd.read_clipboard() # Copy input from question above

df1 = df1.set_index(df1.columns[0]) #Create index with first column

#Create IntervalIndex from ranges in index and column headers
df1.columns = pd.IntervalIndex.from_arrays([0,30,55],[30,55,70]) 
df1.index = pd.IntervalIndex.from_arrays([0,50,75],[50,75,100])

#Test getting value by stacking columns and index to create multiIndex
df1.stack().loc[(40,25)] #returns .1

# Create list of tuples for "lookup" in df1 with zip
df['Ratio Give by grid'] = df1.stack().loc[zip(df['Sum total'], df['Sum partial'])].to_numpy()

df

输出:

ID  Sum total  Sum partial  Ratio Give by grid
0  A1         40           25                0.10
1  A2         70           50                0.18
2  A3        100           40                0.27

相关问题