如何在Pandas中使用GroupBy对负值和正值进行求和
在这篇文章中,我们将讨论如何使用Pandas的GroupBy方法来计算DataFrame中所有负数和正数的总和。
要使用groupby()方法,请使用下面的语法。
语法: df.groupby(column_name)
实现步骤
第1步:创建lambda函数来计算正和与负和的值。
pos = lambda col : col[col > 0].sum()
neg = lambda col : col[col < 0].sum()
第2步:我们将使用groupby()方法并应用lambda函数来计算总和。
d = df.groupby(df['Alphabet'])
print(d['Frequency'].agg([('negative_values', neg),
('positive_values', pos)
]))
print(d['Bandwidth'].agg([('negative_values', neg),
('positive_values', pos)
]))
示例
示例 1:
计算两列(即频率和带宽)的a、b、c的所有正值和负值之和。
# Import Necessary Libraries
import pandas as pd
import numpy as np
# Creating a DataFrame with
# random values
df = pd.DataFrame({'Alphabet': ['a', 'b', 'c', 'c',
'a', 'a', 'c', 'b'],
'Frequency': [-10, 29, -12, -190,
72, -98, -12, 0],
'BandWidth': [10, 34, 23, -10, -87,
-76, 365, 10]})
print(df)
# Group By dataframe on categorical
# values
d = df.groupby(df['Alphabet'])
# creating lambda function to calculate
# positive as well as negative values
def pos(col):
return col[col > 0].sum()
def neg(col):
return col[col < 0].sum()
# Apply lambda function to particular
# column
print(d['Frequency'].agg([('negative_values', neg),
('positive_values', pos)
]))
print(d['Bandwidth'].agg([('negative_values', neg),
('positive_values', pos)
]))
输出:
示例 2:
计算两列(即X和Y)的a、b的所有正值和负值之和。
# Import Necessary Libraries
import pandas as pd
import numpy as np
# Creating a DataFrame with random values
df = pd.DataFrame({'Function': ['F(x)', 'F(x)', 'F(y)',
'F(x)', 'F(y)', 'F(x)',
'F(x)', 'F(y)'],
'X': [-10, 29, -12, -190, 72, -98,
-12, 0],
'Y': [10, 34, 23, -10, -87, -76,
365, 10]})
print(df)
# Group By dataframe on categorical values
d = df.groupby(df['Function'])
# creating lambda function to calculate
# positive as well as negative values
def pos(col):
return col[col > 0].sum()
def neg(col):
return col[col < 0].sum()
# Apply lambda function to particular
# column
print(d['X'].agg([('negative_values', neg),
('positive_values', pos)
]))
print(d['Y'].agg([('negative_values', neg),
('positive_values', pos)
]))
输出:
DataFrame
X 输出
Y 输出
示例 3:
计算每个名字的所有正值以及负值的总和,即Marks。下一步是制作lambda函数来计算总和。在最后一步,我们将根据名字对数据进行分组,并调用lambda函数来计算数值之和。
# Import Necessary Libraries
import pandas as pd
import numpy as np
# Creating a DataFrame with random values
df = pd.DataFrame({'Name': ['Aryan', 'Nityaa', 'Dhruv',
'Dhruv', 'Nityaa', 'Aryan',
'Nityaa', 'Aryan', 'Aryan',
'Dhruv', 'Nityaa', 'Dhruv',
'Dhruv'],
'Marks': [90, 93, 78, 56, 34, 12, 67,
45, 78, 92, 29, 88, 81]})
print(df)
# Group By dataframe on categorical values
d = df.groupby(df['Name'])
# creating lambda function to calculate
# positive as well as negative values
def pos(col):
return col[col > 0].sum()
def neg(col):
return col[col < 0].sum()
# Apply lambda function to particular
# column
print(d['Marks'].agg([('negative_values', neg),
('positive_values', pos)
]))
输出:
Names
Marks