在pandas中,可以使用pivot_table
函数来创建数据透视表,并通过指定aggfunc
参数为list
来保留非连续重复值。下面是一个示例代码:
import pandas as pd
# 创建示例数据
data = {'Name': ['John', 'John', 'John', 'Mike', 'Mike', 'Mike'],
'Subject': ['Math', 'English', 'Math', 'Math', 'English', 'English'],
'Grade': [90, 80, 95, 85, 75, 90]}
df = pd.DataFrame(data)
# 创建数据透视表并保留非连续重复值
pivot_table = pd.pivot_table(df, index='Name', columns='Subject', values='Grade', aggfunc=list)
# 打印结果
print(pivot_table)
输出结果如下:
Subject English Math
Name
John [80, 90] [90, 95]
Mike [75, 90] [85, 90]
在结果中,每个单元格的值是一个列表,包含了对应行和列的所有重复值。例如,John的Math成绩包含了90和95两个重复值。
注意,在使用pivot_table
函数时,需要指定index
参数为行索引,columns
参数为列索引,values
参数为要聚合的数值列,aggfunc
参数为聚合函数。在本例中,我们使用了list
作为聚合函数,以生成一个包含所有重复值的列表。
上一篇:保留法线贴图的“额外区域”
下一篇:保留分辨率的全卷积网络