从Pivoted Panda Dataframe获取列名,而没有原始列表的列名 [英] Get column names from Pivoted Panda Dataframe, without the column name of the original list

查看:615
本文介绍了从Pivoted Panda Dataframe获取列名,而没有原始列表的列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集:

   a        b     c
99-01-11    8   367235
99-01-11    5   419895
99-01-11    1   992194
99-03-23    4   419895
99-04-30    1   992194
99-06-02    9   419895
99-08-08    2   367235
99-08-12    3   419895
99-08-17    10  992194
99-10-22    3   419895
99-12-04    4   992194
00-03-04    2   367235
00-09-29    9   367235
00-09-30    9   367235

我使用以下代码将其更改为数据透视表:

I changed it to a pivot table using the following code:

df = (pd.read_csv('orcs.csv'))

df_wanted = pd.pivot_table(df, index=['c'], columns=['a'], values=['b'])

我的目标:我正在尝试获取数据透视表中列名称的列表.换句话说,我试图得到这个:

My goal: I am trying to get a list of the column names in the pivot table. In other words, I am trying to get this:

['1999-01-11','1999-01-11','1999-01-11','1999-03-23','1999-04-30','1999-06-02','1999-08-08']

我尝试使用这段代码:

y= df_wanted.columns.tolist()

但这给了我一个同时包含原始列名和数据透视表的新列名的列表:

But this gives me a list with both the original column name and the pivot's new column name:

[('c', '00-03-04'), ('c', '00-09-29'), ('c', '00-09-30'), ('c', '99-01-11'), ('c', '99-03-23'), ('c', '99-04-30'), ('c', '99-06-02'), ('c', '99-08-08'), ('c', '99-08-12'), ('c', '99-08-17'), ('c', '99-10-22'), ('c', '99-12-04')]

我尝试了各种删除方式,例如

I tried deleting the 'c' in various ways, such as

def remove_values_from_list(the_list, val):
        while val in the_list:
            the_list.remove(val)
remove_values_from_list(y, 'c')

但是没有运气.有谁知道如何解决这个问题? PS.保留列表的顺序很重要,因为我将其用作折线图的y值数组.

but have had no luck. Does anyone know how to fix this problem? PS. retaining the order of the list is important, as I am going to use it as an array of y values for a line graph.

非常感谢.

推荐答案

最好先在pivot_table中省略[],以避免在列中使用MultiIndex,然后使用tolist()强制转换为string:

The best is first omit [] in pivot_table for avoid MultiIndex in columns and then use tolist() with cast to string:

df_wanted = pd.pivot_table(df,index='c',columns='a',values='b')
#print (df_wanted)

print (df_wanted.columns.astype(str).tolist())
['1999-01-11', '1999-03-23', '1999-04-30', '1999-06-02', '1999-08-08', 
'1999-08-12', '1999-08-17', '1999-10-22', '1999-12-04',
 '2000-03-04', '2000-09-29', '2000-09-30']

这篇关于从Pivoted Panda Dataframe获取列名,而没有原始列表的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆