我希望能够使用一个单独的函数的输出来更新多索引数据帧中的值,该函数在另一个现有数据帧上执行计算。
例如,我有以下内容:
import numpy as np, pandas as pd
names = ['Johnson','Jackson','Smith']
attributes = ['x1','x2','x3','x4','x5']
categories = ['y1','y2','y3','y4','y5','y6']
index = pd.MultiIndex.from_product([names, attributes])
placeholders = np.zeros((len(names)*len(attributes), len(categories)), dtype=int)
df = pd.DataFrame(placeholders, index=index, columns=categories)
哪个生成相应的数据帧:
y1 y2 y3 y4 y5 y6
Johnson x1 0 0 0 0 0 0
x2 0 0 0 0 0 0
x3 0 0 0 0 0 0
x4 0 0 0 0 0 0
x5 0 0 0 0 0 0
Jackson x1 0 0 0 0 0 0
x2 0 0 0 0 0 0
x3 0 0 0 0 0 0
x4 0 0 0 0 0 0
x5 0 0 0 0 0 0
Smith x1 0 0 0 0 0 0
x2 0 0 0 0 0 0
x3 0 0 0 0 0 0
x4 0 0 0 0 0 0
x5 0 0 0 0 0 0
现在,我还有另一个函数可以生成一系列值,然后使用这些值来更新此数据框。例如:
x1 = pd.Series([2274, 556, 1718, 1171, 183, 194], index=categories)
x2 = pd.Series([627, 154, 473, 215, 68, 77], index=categories)
我将如何更新('Johnson','x1')
的系列值?
向量x1
和x2
是通过在两个嵌套的for循环内调用函数而生成的。我似乎无法弄清楚如何更新数据框,这些值只是保持全零:
for i in names:
for j in attributes:
x1 = generate_data_list('x1')
df.loc[i,j].update(x1)
感谢任何帮助!
答案 0 :(得分:1)
只需将x1
分配给df.loc[i, j]
:
df.loc['Johnson', 'x1'] = x1
或者:
df.loc[('Johnson', 'x1')] = x1
df
# y1 y2 y3 y4 y5 y6
#Johnson x1 2274 556 1718 1171 183 194
# x2 0 0 0 0 0 0
# x3 0 0 0 0 0 0
# x4 0 0 0 0 0 0
# x5 0 0 0 0 0 0
#Jackson x1 0 0 0 0 0 0
# x2 0 0 0 0 0 0
# x3 0 0 0 0 0 0
# x4 0 0 0 0 0 0
# x5 0 0 0 0 0 0
#Smith x1 0 0 0 0 0 0
# x2 0 0 0 0 0 0
# x3 0 0 0 0 0 0
# x4 0 0 0 0 0 0
# x5 0 0 0 0 0 0
答案 1 :(得分:1)
您可以使用update
x1 = pd.DataFrame(data=[[2274, 556, 1718, 1171, 183, 194]], index=pd.MultiIndex.from_arrays([['Johnson'],['x1']]),columns=categories)
x1
y1 y2 y3 y4 y5 y6
Johnson x1 2274 556 1718 1171 183 194
df.update(x1)
df
y1 y2 y3 y4 y5 y6
Johnson x1 2274.0 556.0 1718.0 1171.0 183.0 194.0
x2 0.0 0.0 0.0 0.0 0.0 0.0
x3 0.0 0.0 0.0 0.0 0.0 0.0
x4 0.0 0.0 0.0 0.0 0.0 0.0
x5 0.0 0.0 0.0 0.0 0.0 0.0
Jackson x1 0.0 0.0 0.0 0.0 0.0 0.0
x2 0.0 0.0 0.0 0.0 0.0 0.0
x3 0.0 0.0 0.0 0.0 0.0 0.0
x4 0.0 0.0 0.0 0.0 0.0 0.0
x5 0.0 0.0 0.0 0.0 0.0 0.0
Smith x1 0.0 0.0 0.0 0.0 0.0 0.0
x2 0.0 0.0 0.0 0.0 0.0 0.0
x3 0.0 0.0 0.0 0.0 0.0 0.0
x4 0.0 0.0 0.0 0.0 0.0 0.0
x5 0.0 0.0 0.0 0.0 0.0 0.0