访问另一个数据框中的合并数据?
Accessing binned data in another dataframe?
我有两个数据框,一般结构如下:
df2 中的分箱值与 df1 中的“A”对应相同的变量,但基于更大的数据集,并为不同的变量“C”提供值。我想根据 df2 中“C”的正确值调整“B”中的值。例如,第一行的“B”15.3 将从“C”调整值 1.5,因为它的“A”落在容器 [2,4] 中。
虽然我不确定如何访问分箱 df 中的行,因为我无法直接查询具有 df1 的“A”中的确切值的索引。
如果能提供有关如何访问此数据的任何提示,我们将不胜感激。
编辑:抱歉,我现在添加了一个代码示例:
设置:
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'A':[4.4, 3.6, 9.2, 3.4], 'B':[15.3, 10.8, 10.3, 17.0]})
df2 = pd.DataFrame({'A':[0.0, 4.9, 9.3, 4.5, 2.9, 3.2, 1.0, 6.7, 8.7, 9.8, 3.4, .7, 2.2, 6.5, 3.4, 1.7, 9.4, 10.0],
'C':[1.3, 4.3, 4.8, 3.5, 1.7, 2.2, 1.1, 4.9, 5.6, 5.6, 2.5, .6, 1.9, 4.1, 3.6, 2.8, 6.3, 5.9]})
bins = np.arange(df2['A'].min(), df2['A'].max()+2, 2)
df2 = df2.groupby(pd.cut(df2['A'], bins)).mean()
然后我的想法是为 DF1 中的每个 A 值确定适当的 bin,然后以某种方式引用它。第一行用于执行此操作,但第二步(使用此 bin 查询 df2)不会:
df1['Bin']=pd.cut(df1['A'], bins)
df1['Product'] = df1['B'] * df2.loc(df1['Bin'])['C']
我从第二行得到的错误是“TypeError: 'Series' objects are mutable, thus they cannot be hashed."
我想要的输出是一个“产品”列,它将“B”中的值乘以“C”中的正确值,基于“A”值属于哪个 bin。
想法是让 df1 中 'A' 中的每个元素从 df2 中找到相应的 bin 的(索引),然后合并。这可能可以更优雅地完成,但似乎有效。我最终更改了 df2 中的一些列名称,希望它没问题。完整代码:
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'A':[4.4, 3.6, 9.2, 3.4], 'B':[15.3, 10.8, 10.3, 17.0]})
df2 = pd.DataFrame({'A':[0.0, 4.9, 9.3, 4.5, 2.9, 3.2, 1.0, 6.7, 8.7, 9.8, 3.4, .7, 2.2, 6.5, 3.4, 1.7, 9.4, 10.0],
'C':[1.3, 4.3, 4.8, 3.5, 1.7, 2.2, 1.1, 4.9, 5.6, 5.6, 2.5, .6, 1.9, 4.1, 3.6, 2.8, 6.3, 5.9]})
bins = np.arange(df2['A'].min(), df2['A'].max()+2, 2)
df3 = df2.groupby(pd.cut(df2['A'], bins)).mean()
# Here I am resetting the index on df3 and renaming the bin column to bins
df3 = df3.rename_axis('bins').reset_index()
# The main calc of matching the bin.
match_indx = [[a in interval for interval in df3['bins']].index(True) for a in df1['A']]
# stick in into df1
df1['bin_index'] = match_indx
# merge
df4 = df1.merge(df3, left_on = 'bin_index', right_index=True)
df4
现在 df4 看起来像这样
A_x B bin_index bins A_y C
0 4.4 15.3 2 (4.0, 6.0] 4.70 3.90
1 3.6 10.8 1 (2.0, 4.0] 3.02 2.38
3 3.4 17.0 1 (2.0, 4.0] 3.02 2.38
2 9.2 10.3 4 (8.0, 10.0] 9.44 5.64
现在我们可以为您计算了。 df1 中的列 A
已重命名为 A_x
(如果需要,您可以将其重命名,我没有打扰)
df4['Product'] = df4['B'] * df4['C']
匹配逻辑在传统的循环格式中更容易理解
match_indx = []
for a in df1['A']:
# binary_mask will have True or False depending on whether a is in df3['bins'][i] or not
binary_mask = []
for interval in df3['bins']:
binary_mask.append(a in interval)
# find the first (and only) item that is True in binary_mask. Return its index
index = binary_mask.index(True)
match_indx.append(index)
我有两个数据框,一般结构如下:
df2 中的分箱值与 df1 中的“A”对应相同的变量,但基于更大的数据集,并为不同的变量“C”提供值。我想根据 df2 中“C”的正确值调整“B”中的值。例如,第一行的“B”15.3 将从“C”调整值 1.5,因为它的“A”落在容器 [2,4] 中。
虽然我不确定如何访问分箱 df 中的行,因为我无法直接查询具有 df1 的“A”中的确切值的索引。
如果能提供有关如何访问此数据的任何提示,我们将不胜感激。
编辑:抱歉,我现在添加了一个代码示例:
设置:
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'A':[4.4, 3.6, 9.2, 3.4], 'B':[15.3, 10.8, 10.3, 17.0]})
df2 = pd.DataFrame({'A':[0.0, 4.9, 9.3, 4.5, 2.9, 3.2, 1.0, 6.7, 8.7, 9.8, 3.4, .7, 2.2, 6.5, 3.4, 1.7, 9.4, 10.0],
'C':[1.3, 4.3, 4.8, 3.5, 1.7, 2.2, 1.1, 4.9, 5.6, 5.6, 2.5, .6, 1.9, 4.1, 3.6, 2.8, 6.3, 5.9]})
bins = np.arange(df2['A'].min(), df2['A'].max()+2, 2)
df2 = df2.groupby(pd.cut(df2['A'], bins)).mean()
然后我的想法是为 DF1 中的每个 A 值确定适当的 bin,然后以某种方式引用它。第一行用于执行此操作,但第二步(使用此 bin 查询 df2)不会:
df1['Bin']=pd.cut(df1['A'], bins)
df1['Product'] = df1['B'] * df2.loc(df1['Bin'])['C']
我从第二行得到的错误是“TypeError: 'Series' objects are mutable, thus they cannot be hashed."
我想要的输出是一个“产品”列,它将“B”中的值乘以“C”中的正确值,基于“A”值属于哪个 bin。
想法是让 df1 中 'A' 中的每个元素从 df2 中找到相应的 bin 的(索引),然后合并。这可能可以更优雅地完成,但似乎有效。我最终更改了 df2 中的一些列名称,希望它没问题。完整代码:
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'A':[4.4, 3.6, 9.2, 3.4], 'B':[15.3, 10.8, 10.3, 17.0]})
df2 = pd.DataFrame({'A':[0.0, 4.9, 9.3, 4.5, 2.9, 3.2, 1.0, 6.7, 8.7, 9.8, 3.4, .7, 2.2, 6.5, 3.4, 1.7, 9.4, 10.0],
'C':[1.3, 4.3, 4.8, 3.5, 1.7, 2.2, 1.1, 4.9, 5.6, 5.6, 2.5, .6, 1.9, 4.1, 3.6, 2.8, 6.3, 5.9]})
bins = np.arange(df2['A'].min(), df2['A'].max()+2, 2)
df3 = df2.groupby(pd.cut(df2['A'], bins)).mean()
# Here I am resetting the index on df3 and renaming the bin column to bins
df3 = df3.rename_axis('bins').reset_index()
# The main calc of matching the bin.
match_indx = [[a in interval for interval in df3['bins']].index(True) for a in df1['A']]
# stick in into df1
df1['bin_index'] = match_indx
# merge
df4 = df1.merge(df3, left_on = 'bin_index', right_index=True)
df4
现在 df4 看起来像这样
A_x B bin_index bins A_y C
0 4.4 15.3 2 (4.0, 6.0] 4.70 3.90
1 3.6 10.8 1 (2.0, 4.0] 3.02 2.38
3 3.4 17.0 1 (2.0, 4.0] 3.02 2.38
2 9.2 10.3 4 (8.0, 10.0] 9.44 5.64
现在我们可以为您计算了。 df1 中的列 A
已重命名为 A_x
(如果需要,您可以将其重命名,我没有打扰)
df4['Product'] = df4['B'] * df4['C']
匹配逻辑在传统的循环格式中更容易理解
match_indx = []
for a in df1['A']:
# binary_mask will have True or False depending on whether a is in df3['bins'][i] or not
binary_mask = []
for interval in df3['bins']:
binary_mask.append(a in interval)
# find the first (and only) item that is True in binary_mask. Return its index
index = binary_mask.index(True)
match_indx.append(index)