计算上面各行的平方和
calculate sum of squares with rows above
我有一个如下所示的数据集:
Value Type X_sq
-1.975767 Weather
-0.540979 Fruits
-2.359127 Fruits
-2.815604 Corona
-0.929755 Weather
我想遍历每一行并计算上面每一行的平方和值(仅当类型匹配时)。我想把这个值放在 X.sq 列中。
因此,例如,在第一行中,上面没有任何内容。所以只有 (-1.975767 x -1.975767)。在第二行中,上面没有 FRUITS 行,因此它只是 -0.540979 x -0.540979。然而,在第三行,当我们扫描所有前面的行时,我们应该发现 FRUITS 已经在那里了。所以我们应该获取最后一个FRUIT的.....X_sq值并计算新的平方和。
Value Type X_sq
-1.975767 Weather -1.975767 * -1.975767 = x
-0.540979 Fruits -0.540979 * -0.540979 = y
-2.359127 Fruits y + ( -2.359127 x -2.359127)
-2.815604 Corona -2.815604 * -2.815604
-0.929755 Weather x + (-0.929755 * -0.929755)
执行此操作的有效方法是什么?
def updateSS(X_sq, X_new):
return X_sq + X_new**2
编辑:
----> 1 df['sumOfSquares'] = df['avg_country_tone'].pow(2).groupby(['themes', 'suppliers_country']).cumsum()
File /usr/local/Cellar/ipython/8.0.1/libexec/lib/python3.10/site-packages/pandas/core/series.py:1929, in Series.groupby(self, by, axis, level, as_index, sort, group_keys, squeeze, observed, dropna)
1925 axis = self._get_axis_number(axis)
1927 # error: Argument "squeeze" to "SeriesGroupBy" has incompatible type
1928 # "Union[bool, NoDefault]"; expected "bool"
-> 1929 return SeriesGroupBy(
1930 obj=self,
1931 keys=by,
1932 axis=axis,
1933 level=level,
1934 as_index=as_index,
1935 sort=sort,
1936 group_keys=group_keys,
1937 squeeze=squeeze, # type: ignore[arg-type]
1938 observed=observed,
1939 dropna=dropna,
1940 )
File /usr/local/Cellar/ipython/8.0.1/libexec/lib/python3.10/site-packages/pandas/core/groupby/groupby.py:882, in GroupBy.__init__(self, obj, keys, axis, level, grouper, exclusions, selection, as_index, sort, group_keys, squeeze, observed, mutated, dropna)
879 if grouper is None:
880 from pandas.core.groupby.grouper import get_grouper
--> 882 grouper, exclusions, obj = get_grouper(
883 obj,
884 keys,
885 axis=axis,
886 level=level,
887 sort=sort,
888 observed=observed,
889 mutated=self.mutated,
890 dropna=self.dropna,
891 )
893 self.obj = obj
894 self.axis = obj._get_axis_number(axis)
File /usr/local/Cellar/ipython/8.0.1/libexec/lib/python3.10/site-packages/pandas/core/groupby/grouper.py:882, in get_grouper(obj, key, axis, level, sort, observed, mutated, validate, dropna)
880 in_axis, level, gpr = False, gpr, None
881 else:
--> 882 raise KeyError(gpr)
883 elif isinstance(gpr, Grouper) and gpr.key is not None:
884 # Add key to exclusions
885 exclusions.add(gpr.key)
KeyError: 'themes'
even though themes is there. Themes = type
使用:
df['X_sq'] = df['Value'].pow(2).groupby(df['Type']).cumsum()
print(df)
# Output
Value Type X_sq
0 -1.975767 Weather 3.903655
1 -0.540979 Fruits 0.292658
2 -2.359127 Fruits 5.858138
3 -2.815604 Corona 7.927626
4 -0.929755 Weather 4.768100
您首先为具有相同类型的连续行构建一个 id
groupid = (df['Type'] != df['Type'].shift()).cumsum()
然后您可以在其上对数据框进行分组,并在每个组上再次使用 cumsum
:
df['X_sq'] = df.groupby(groupid)['Value'].transform(lambda x: (x*x).cumsum())
你应该得到预期的结果:
Value Type X_sq
0 -1.975767 Weather 3.903655
1 -0.540979 Fruits 0.292658
2 -2.359127 Fruits 5.858138
3 -2.815604 Corona 7.927626
4 -0.929755 Weather 0.864444
我有一个如下所示的数据集:
Value Type X_sq
-1.975767 Weather
-0.540979 Fruits
-2.359127 Fruits
-2.815604 Corona
-0.929755 Weather
我想遍历每一行并计算上面每一行的平方和值(仅当类型匹配时)。我想把这个值放在 X.sq 列中。
因此,例如,在第一行中,上面没有任何内容。所以只有 (-1.975767 x -1.975767)。在第二行中,上面没有 FRUITS 行,因此它只是 -0.540979 x -0.540979。然而,在第三行,当我们扫描所有前面的行时,我们应该发现 FRUITS 已经在那里了。所以我们应该获取最后一个FRUIT的.....X_sq值并计算新的平方和。
Value Type X_sq
-1.975767 Weather -1.975767 * -1.975767 = x
-0.540979 Fruits -0.540979 * -0.540979 = y
-2.359127 Fruits y + ( -2.359127 x -2.359127)
-2.815604 Corona -2.815604 * -2.815604
-0.929755 Weather x + (-0.929755 * -0.929755)
执行此操作的有效方法是什么?
def updateSS(X_sq, X_new):
return X_sq + X_new**2
编辑:
----> 1 df['sumOfSquares'] = df['avg_country_tone'].pow(2).groupby(['themes', 'suppliers_country']).cumsum()
File /usr/local/Cellar/ipython/8.0.1/libexec/lib/python3.10/site-packages/pandas/core/series.py:1929, in Series.groupby(self, by, axis, level, as_index, sort, group_keys, squeeze, observed, dropna)
1925 axis = self._get_axis_number(axis)
1927 # error: Argument "squeeze" to "SeriesGroupBy" has incompatible type
1928 # "Union[bool, NoDefault]"; expected "bool"
-> 1929 return SeriesGroupBy(
1930 obj=self,
1931 keys=by,
1932 axis=axis,
1933 level=level,
1934 as_index=as_index,
1935 sort=sort,
1936 group_keys=group_keys,
1937 squeeze=squeeze, # type: ignore[arg-type]
1938 observed=observed,
1939 dropna=dropna,
1940 )
File /usr/local/Cellar/ipython/8.0.1/libexec/lib/python3.10/site-packages/pandas/core/groupby/groupby.py:882, in GroupBy.__init__(self, obj, keys, axis, level, grouper, exclusions, selection, as_index, sort, group_keys, squeeze, observed, mutated, dropna)
879 if grouper is None:
880 from pandas.core.groupby.grouper import get_grouper
--> 882 grouper, exclusions, obj = get_grouper(
883 obj,
884 keys,
885 axis=axis,
886 level=level,
887 sort=sort,
888 observed=observed,
889 mutated=self.mutated,
890 dropna=self.dropna,
891 )
893 self.obj = obj
894 self.axis = obj._get_axis_number(axis)
File /usr/local/Cellar/ipython/8.0.1/libexec/lib/python3.10/site-packages/pandas/core/groupby/grouper.py:882, in get_grouper(obj, key, axis, level, sort, observed, mutated, validate, dropna)
880 in_axis, level, gpr = False, gpr, None
881 else:
--> 882 raise KeyError(gpr)
883 elif isinstance(gpr, Grouper) and gpr.key is not None:
884 # Add key to exclusions
885 exclusions.add(gpr.key)
KeyError: 'themes'
even though themes is there. Themes = type
使用:
df['X_sq'] = df['Value'].pow(2).groupby(df['Type']).cumsum()
print(df)
# Output
Value Type X_sq
0 -1.975767 Weather 3.903655
1 -0.540979 Fruits 0.292658
2 -2.359127 Fruits 5.858138
3 -2.815604 Corona 7.927626
4 -0.929755 Weather 4.768100
您首先为具有相同类型的连续行构建一个 id
groupid = (df['Type'] != df['Type'].shift()).cumsum()
然后您可以在其上对数据框进行分组,并在每个组上再次使用 cumsum
:
df['X_sq'] = df.groupby(groupid)['Value'].transform(lambda x: (x*x).cumsum())
你应该得到预期的结果:
Value Type X_sq
0 -1.975767 Weather 3.903655
1 -0.540979 Fruits 0.292658
2 -2.359127 Fruits 5.858138
3 -2.815604 Corona 7.927626
4 -0.929755 Weather 0.864444