python – Pandas:Set_index函数不会删除列
发布时间:2020-12-20 11:27:42 所属栏目:Python 来源:网络整理
导读:我有以下DataFrame: df = pd.DataFrame({'Trader': 'Carl Mark Carl Joe Joe Carl Joe Carl'.split(),'Product': list('AAAABBAA'),'Quantity': [5,2,5,10,1,3],'Start' : [ DT.datetime(2013,9,0),DT.datetime(2013,8,5),14,16,20,50),7,4,0)]}) 当我尝试
我有以下DataFrame:
df = pd.DataFrame({ 'Trader': 'Carl Mark Carl Joe Joe Carl Joe Carl'.split(),'Product': list('AAAABBAA'),'Quantity': [5,2,5,10,1,3],'Start' : [ DT.datetime(2013,9,0),DT.datetime(2013,8,5),14,16,20,50),7,4,0)]}) 当我尝试通过以下方式输入索引时: df = df.set_index([df.Start,df.Trader,df.Product]) 它不会删除用于索引的列.即使我明确指定它使用: df = df.set_index([df.Start,df.Product],drop=True) 这是一个错误还是我的代码中有错误? 谢谢 安迪 解决方法
该参数是列名称数组,但不是那些系列:
In [9]: df.set_index(['Start','Trader','Product']) Out[9]: Quantity Start Trader Product 2013-01-01 09:00:00 Carl A 5 2013-01-01 08:05:00 Mark A 2 2013-02-05 14:00:00 Carl A 5 2013-02-05 16:00:00 Joe A 10 2013-02-08 20:00:00 Joe B 1 2013-02-08 16:50:00 Carl B 5 2013-02-08 07:00:00 Joe A 2 2013-07-04 08:00:00 Carl A 3 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |