[英]Multiply all columns of a multi-indexed DataFrame by appropriate values in a Series
[英]Updating a multi-indexed dataframe with a list (or series) of values
我希望能够使用在另一个现有数据帧上执行计算的单独函数的输出来更新多索引数据帧中的值。
举例来说,我有以下内容:
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)
感谢任何帮助!
只需将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
您可以使用正确的格式创建信息,然后使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.