[英]pandas pivot table for heatmap
I am trying to generate a heatmap using seaborn, however I am having a small problem with the formatting of my data. 我正在尝试使用seaborn生成热图,但是我的数据格式存在一个小问题。
Currently, my data is in the form: 目前,我的数据格式为:
Name Diag Date
A 1 2006-12-01
A 1 1994-02-12
A 2 2001-07-23
B 2 1999-09-12
B 1 2016-10-12
C 3 2010-01-20
C 2 1998-08-20
I would like to create a heatmap (preferably in python) showing Name
on one axis against Diag
- if occured. 我想创建一个热图(最好在python中),以在与Diag
一个轴上显示Name
(如果发生)。 I have tried to pivot the table using pd.pivot
, however I was given the error 我尝试使用pd.pivot
旋转表,但是出现错误
ValueError: Index contains duplicate entries, cannot reshape ValueError:索引包含重复的条目,无法重塑
this came from: 来自:
piv = df.pivot_table(index='Name',columns='Diag') piv = df.pivot_table(index ='Name',columns ='Diag')
Time is irrelevant, but I would like to show which Names
have had which Diag
, and which Diag
combos cluster together. 时间无关紧要,但是我想展示一下哪些Names
具有哪些Diag
以及哪些Diag
组合在一起。 Do I need to create a new table for this or is it possible for that I have? 我是否需要为此创建一个新表? In some cases the Name
is not associated with all Diag
在某些情况下, Name
未与所有Diag
相关联
EDIT: I have since tried: piv = df.pivot_table(index='Name',columns='Diag', values='Time', aggfunc='mean') 编辑:我从此尝试过:piv = df.pivot_table(index ='Name',columns ='Diag',values ='Time',aggfunc ='mean')
However as Time is in datetime format, I end up with: 但是,由于时间采用日期时间格式,因此我得出以下结论:
pandas.core.base.DataError: No numeric types to aggregate pandas.core.base.DataError:没有要聚合的数字类型
You need pivot_table
with some aggregate function, because for same index and column have multiple values and pivot
need unique values only: 您需要带有一些聚合函数的pivot_table
,因为对于相同的索引和列,它具有多个值,而pivot
仅需要唯一的值:
print (df)
Name Diag Time
0 A 1 12 <-duplicates for same A, 1 different value
1 A 1 13 <-duplicates for same A, 1 different value
2 A 2 14
3 B 2 18
4 B 1 1
5 C 3 9
6 C 2 8
df = df.pivot_table(index='Name',columns='Diag', values='Time', aggfunc='mean')
print (df)
Diag 1 2 3
Name
A 12.5 14.0 NaN
B 1.0 18.0 NaN
C NaN 8.0 9.0
Alternative solution: 替代解决方案:
df = df.groupby(['Name','Diag'])['Time'].mean().unstack()
print (df)
Diag 1 2 3
Name
A 12.5 14.0 NaN
B 1.0 18.0 NaN
C NaN 8.0 9.0
EDIT: 编辑:
You can also check all duplicates by duplicated
: 您还可以按duplicated
检查所有重复duplicated
:
df = df.loc[df.duplicated(['Name','Diag'], keep=False), ['Name','Diag']]
print (df)
Name Diag
0 A 1
1 A 1
EDIT: 编辑:
mean
of datetimes is not easy - need convert dates to nanoseconds
, get mean and last convert to datetimes. 日期时间的mean
并不容易-需要将日期转换为nanoseconds
,获取平均值并最后转换为日期时间。 Also there is another problem - need replace NaN
to some scalar, eg 0
what is converted to 0
datetime - 1970-01-01
. 另外还有一个问题-需要将NaN
替换为某个标量,例如0
转换为0
datetime- 1970-01-01
。
df.Date = pd.to_datetime(df.Date)
df['dates_in_ns'] = pd.Series(df.Date.values.astype(np.int64), index=df.index)
df = df.pivot_table(index='Name',
columns='Diag',
values='dates_in_ns',
aggfunc='mean',
fill_value=0)
df = df.apply(pd.to_datetime)
print (df)
Diag 1 2 3
Name
A 2000-07-07 12:00:00 2001-07-23 1970-01-01
B 2016-10-12 00:00:00 1999-09-12 1970-01-01
C 1970-01-01 00:00:00 1998-08-20 2010-01-20
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.