简体   繁体   English

在熊猫中枢之后重新索引

[英]Reindexing after a pivot in pandas

Consider the following dataset: 考虑以下数据集:

数据集

After running the code: 运行代码后:

convert_dummy1 = convert_dummy.pivot(index='Product_Code', columns='Month', values='Sales').reset_index()

The data is in the right form, but my index column is named 'Month', and I cannot seem to remove this at all. 数据格式正确,但是我的索引列名为“月”,我似乎根本无法删除它。 I have tried codes such as the below, but it does not do anything. 我已经尝试过以下代码,但是它什么也没做。

del convert_dummy1.index.name

I can save the dataset to a csv, delete the ID column, and then read the csv - but there must be a more efficient way. 我可以将数据集保存到csv,删除ID列,然后读取csv-但必须有一种更有效的方法。

Dataset after reset_index(): reset_index()之后的数据集:

在此处输入图片说明

convert_dummy1

Month   Product_Code      0   1   2   3   4
    0        10133.9      0   0   0   0   0
    1        10146.9    120  80  60   0 100

convert_dummy1.index = pd.RangeIndex(len(convert_dummy1.index))
del convert_dummy1.columns.name
convert_dummy1

    Product_Code      0   1   2   3   4
0        10133.9      0   0   0   0   0
1        10146.9    120  80  60   0 100

Since you pivot with columns="Month" , each column in output corresponds to a month. 由于您使用columns="Month"透视,因此输出中的每一列都对应一个月。 If you decide to reset index after the pivot, you should check column names with convert_dummy1.columns.value which should return in your case : 如果您决定在数据透视之后重设索引,则应使用convert_dummy1.columns.value检查列名,该列名将在您的情况下返回:

array(['Product_Code', 1, 2, 3, 4, 5], dtype=object)

while convert_dummy1.columns.names should return: convert_dummy1.columns.names应该返回:

FrozenList(['Month'])

So to rename Month , use rename_axis function: 因此,要重命名Month ,请使用rename_axis函数:

convert_dummy1.rename_axis('index',axis=1)

Output: 输出:

index   Product_Code    1   2   3   4   5
0       10133           NaN NaN NaN NaN 0.0
1       10234           NaN 0.0 NaN NaN NaN
2       10245           0.0 NaN NaN NaN NaN
3       10345           NaN NaN NaN 0.0 NaN
4       10987           NaN NaN 1.0 NaN NaN

If you wish to reproduce it, this is my code: 如果您想复制它,这是我的代码:

df1=pd.DataFrame({'Product_Code':[10133,10245,10234,10987,10345], 'Month': [1,2,3,4,5], 'Sales': [0,0,0,1,0]})
df2=df1.pivot_table(index='Product_Code', columns='Month', values='Sales').reset_index().rename_axis('index',axis=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM