Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过删除某些标题和重命名某些索引进行数据透视_Python_Pandas_Pivot - Fatal编程技术网

Python 通过删除某些标题和重命名某些索引进行数据透视

Python 通过删除某些标题和重命名某些索引进行数据透视,python,pandas,pivot,Python,Pandas,Pivot,我有以下数据帧: count event date 0 1544 'strike' 2016-11-01 1 226 'defense' 2016-11-01 2 1524 'strike' 2016-12-01 3 246 'defense' 2016-12-01 4 1592 'strike' 2017-01-01 5 245 'defense' 2017-01-01 我希望以这样的方式对其进行透视

我有以下数据帧:

    count  event      date
0    1544  'strike'   2016-11-01
1     226  'defense'  2016-11-01
2    1524  'strike'   2016-12-01
3     246  'defense'  2016-12-01
4    1592  'strike'   2017-01-01
5     245  'defense'  2017-01-01
我希望以这样的方式对其进行透视/变换最终输出如下所示:

event     2016-11-01 2016-12-01 2017-01-01 2017-02-01 2017-03-01                                                                     
'strike'        1544       1524       1592       1608       1654   
'defense'        226        246        245        210        254
但我现在通过旋转得到的是:

                        count          count            count           count             count\
date          2016-11-01 2016-12-01 2017-01-01 2017-02-01 2017-03-01   
event                                                                  
'strike'            1544       1524       1592       1608       1654   
'defense'            226        246        245        210        254   
是否有任何方法可以删除
事件
索引名称前面的整个空行,并使用
事件
作为索引名称重命名
日期
索引名称,还可以删除数据框第一行中出现的不需要的
计数
?数据似乎正在正确地转换,我只是想去掉这些头和索引,并正确地重命名和删除这些头和索引。我也不希望行标签出现在所需的输出中

这就是我一直在尝试的:

output = df.pivot(index='event', columns='date')
print(output)

解决方案是将参数
值添加到,然后从
索引中为列添加,然后从中删除列名:

output=df.pivot(index='event',columns='date',values='count').reset_index().rename_axis(None,1)
print(output)
       event  2016-11-01  2016-12-01  2017-01-01
0  'defense'         226         246         245
1   'strike'        1544        1524        1592
如果忽略它会发生什么

print (df)
   count      event        date  count1
0   1544   'strike'  2016-11-01       1
1    226  'defense'  2016-11-01       7
2   1524   'strike'  2016-12-01       8
3    246  'defense'  2016-12-01       3
4   1592   'strike'  2017-01-01       0
5    245  'defense'  2017-01-01       1
pivot
使用每个未使用的列并为区分原始列创建
多索引

output = df.pivot(index='event', columns='date')
print(output)
               count                           count1                      
date      2016-11-01 2016-12-01 2017-01-01 2016-11-01 2016-12-01 2017-01-01
event                                                                      
'defense'        226        246        245          7          3          1
'strike'        1544       1524       1592          1          8          0

我建议使用更通用的
pd.pivot()
,即
pd.pivot\u table()
,如下所示:

x = pd.pivot_table(df, index = 'event', columns = 'date', values = 'count')
您将获得:

date    01/01/2017  01/11/2016  01/12/2016
event           
'defense'   245         226        246
'strike'    1592        1544       1524
接下来,您可以通过设置以下内容来删除“日期”字符串:

x.columns.name = ' ' 
此外,如果要更改事件顺序,可能需要在执行数据透视之前将变量设置为分类变量:

df.event = df.event.astype('category') # cast to categorical
df.event.cat.set_categories(your_list, inplace = True) # force order
其中,
your_list
是您的类别列表,按顺序排列


希望这有帮助。

你的pivot的代码是什么?@jezrael my bad,忘记添加代码了。现在编辑我非常不喜欢这个解决方案,因为如果使用
pivot
并得到重复的,它知道,因为错误。但是如果使用
pivot\u表
,它总是聚合。所以不知道什么是骗局。因此,只有当需要聚合时才更好,否则就不行。另外,它在
pivot
@AmanSingh时速度较慢-是的,你是对的,
pivot\u table
aggregate。但如果需要,请删除
0,1
,并使用
output=df.pivot(index='event',columns='date',values='count')。重置索引()。使用
output重命名轴(None,1)
。到\u csv(file,index=False)
所有操作都很好。今天使用了这种方法,2年后它工作得很好:)