在 Pandas 聚合中计数不同
在本文中,让我们看看如何在 Pandas 聚合中计算不同。所以为了计算 pandas 聚合中的不同,我们将使用 groupby() 和 add() 方法。
- 通过...分组(): 此方法用于根据某些标准将数据分组。 Pandas 对象可以在它们的任何轴上拆分。我们可以创建一组类别并将函数应用于这些类别。分组的抽象定义是提供标签到组名的映射
- agg():此方法用于传递一个函数或函数列表,以分别应用于系列甚至系列的每个元素。在函数列表的情况下,agg() 方法返回多个结果。
以下是一些描述如何在 Pandas 聚合中计算不同值的示例:
示例 1:
Python
# import module
import pandas as pd
import numpy as np
# create Data frame
df = pd.DataFrame({'Video_Upload_Date': ['2020-01-17',
'2020-01-17',
'2020-01-19',
'2020-01-19',
'2020-01-19'],
'Viewer_Id': ['031', '031', '032',
'032', '032'],
'Watch_Time': [34, 43, 43, 41, 40]})
# print original Dataframe
print(df)
# let's Count distinct in Pandas aggregation
df = df.groupby("Video_Upload_Date").agg(
{"Watch_Time": np.sum, "Viewer_Id": pd.Series.nunique})
# print final output
print(df)
Python
# import module
import pandas as pd
import numpy as np
# create Data frame
df = pd.DataFrame({'Order Date': ['2021-02-22',
'2021-02-22',
'2021-02-22',
'2021-02-24',
'2021-02-24'],
'Product Id': ['021', '021',
'022', '022', '022'],
'Order Quantity': [23, 22, 22,
45, 10]})
# print original Dataframe
print(df)
# let's Count distinct in Pandas aggregation
df = df.groupby("Order Date").agg({"Order Quantity": np.sum,
"Product Id": pd.Series.nunique})
# print final output
print(df)
输出:
示例 2:
Python
# import module
import pandas as pd
import numpy as np
# create Data frame
df = pd.DataFrame({'Order Date': ['2021-02-22',
'2021-02-22',
'2021-02-22',
'2021-02-24',
'2021-02-24'],
'Product Id': ['021', '021',
'022', '022', '022'],
'Order Quantity': [23, 22, 22,
45, 10]})
# print original Dataframe
print(df)
# let's Count distinct in Pandas aggregation
df = df.groupby("Order Date").agg({"Order Quantity": np.sum,
"Product Id": pd.Series.nunique})
# print final output
print(df)
输出: