如何将计算结果添加到带有字典的数据框中?

how to add results of calculation into a dataframe with dictionary?

我遇到以下问题:尝试将 'time' 和 'y_corrected' 添加到新数据帧时出现错误。

我需要计算一个变量,'y_corrected',并将它添加到一个新的数据框中。为了计算这个变量,我使用 group 函数根据两个标准循环遍历数据集:文件名和处理。最终数据框应包含文件名、处理、时间、y_corrected.

file = pd.read_excel(r'C:.....xlsx')
grouped = file.groupby(['File name', 'Treatment'])

########################################  output dataframe #####################################
new = pd.DataFrame(columns=['File name','Treatment', 'Time', 'y_corrected'])
new.columns = ['File name', 'Treatment', 'Time', 'y_corrected']

######################################## correction ########################################
for key, g in grouped:
  a = g['y'].max()
  b = g['y'].min()

  y_corrected = (g['y'] - b) / a

  row = {'File name': key[0], 'Treatment': key[1],  'Time': time[2], 'y_corrected': y_corrected[3]}
  new = new.append(row, ignore_index=True)

print(new)

这是错误: 结果 = self.index.get_value(self, key)

为了快速解决问题,您可以尝试将值作为列表传递到“行”对象中,我之前在使用 dataframe.append() 时遇到过一些类似的问题。例如:

row = {'File name': [key[0]], 'Treatment': [key[1]] ....

因为g['y']是一个级数,y_corrected也是一个等于一组长度的级数

y_corrected = (g['y'] - b) / a

y_corrected[3] 不起作用。您应该再次遍历这些值以获取行值(我遗漏了 'time' 因为这似乎与问题无关)。

import pandas as pd

df = pd.DataFrame([['A', 'Z', 1.0],
                     ['A', 'Z', 0.5],
                     ['A', 'Y', 1.5],
                     ['A', 'Y', 0.5],
                     ['B', 'Z', 1.0],
                     ['B', 'Z', 0.5],
                     ['B', 'Y', 1.5],
                     ['B', 'Y', 0.5],
                     ],
                    columns=['File name', 'Treatment', 'y']
                    )
grouped = df.groupby(['File name', 'Treatment'])

########################################  output dataframe #####################################
new = pd.DataFrame(columns=['File name', 'Treatment', 'y_corrected'])
new.columns = ['File name', 'Treatment', 'y_corrected']

######################################## correction ########################################
for key, g in grouped:
    a = g['y'].max()
    b = g['y'].min()

    y_corrected = (g['y'] - b) / a

    for idx, y_corrected_ in y_corrected.items():
        row = {'File name': key[0], 'Treatment': key[1], 'y_corrected': y_corrected_}
        new = new.append(row, ignore_index=True)

解决此问题的更简单方法是直接对您的组执行操作。

def correction(s):
    return (s - s.min()) / s.max()

df['y_corrected'] = grouped.apply(correction)

print(df)

给出:

  File name Treatment    y  y_corrected
0         A         Z  1.0     0.500000
1         A         Z  0.5     0.000000
2         A         Y  1.5     0.666667
3         A         Y  0.5     0.000000
4         B         Z  1.0     0.500000
5         B         Z  0.5     0.000000
6         B         Y  1.5     0.666667
7         B         Y  0.5     0.000000

您不必遍历不同的组。您只需要在数据框上使用 pandas 魔法:

file = pd.read_excel(r'C:.....xlsx')

file['y_corrected'] = file.groupby(['File name', 'Treatment'])['y'].apply(lambda x: (x-min(x))/max(x))