简体   繁体   English

使用值列表(或系列)更新多索引数据框

[英]Updating a multi-indexed dataframe with a list (or series) of values

I want to be able to update the values in a multi-indexed dataframe, using the output of a separate function that performs calculations on another existing dataframe. 我希望能够使用在另一个现有数据帧上执行计算的单独函数的输出来更新多索引数据帧中的值。

Let's say for example, I have the following: 举例来说,我有以下内容:

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)

Which generates the corresponding dataframe: 生成相应的数据框:

            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

Now, I have another function that generates a series of values that I want to then use to update this dataframe. 现在,我有另一个函数,该函数生成一系列值,然后将其用于更新此数据框。 For example: 例如:

x1 = pd.Series([2274, 556, 1718, 1171, 183, 194], index=categories)
x2 = pd.Series([627, 154, 473, 215, 68, 77], index=categories)

How would I go about updating the series values for ('Johnson','x1') ? 我将如何更新('Johnson','x1')的序列值?

The vectors x1 and x2 are generated by calling the function inside of two nested for loops. 向量x1x2是通过在两个嵌套的for循环内调用函数而生成的。 I can't seem to figure out how to update the dataframe, the values just remain all zeros: 我似乎无法弄清楚如何更新数据框,这些值只是保持全零:

for i in names:
    for j in attributes:
        x1 = generate_data_list('x1')
        df.loc[i,j].update(x1)

Appreciate any help! 感谢任何帮助!

Just assign x1 to df.loc[i, j] : 只需将x1分配给df.loc[i, j]

df.loc['Johnson', 'x1'] = x1

Or: 要么:

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

You can create the information in the right format then using update 您可以使用正确的格式创建信息,然后使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM