Pandas,Pivot 错误 - 无法使用空键标记索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41396645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:41:25  来源:igfitidea点击:

Pandas, Pivot error - cannot label index with null key

pythonpandas

提问by Haritha

I have a data set which looks like below.

我有一个如下所示的数据集。

ID     Product   date
1        A     01.01.2015  
1        B     01.01.2015  
1        C     01.03.2015  

A person can take more than one product on the same day, I want to transpose it by ID,date and get product as values.

一个人可以在同一天携带多个产品,我想通过 ID、日期将其转置并获取产品作为值。

ID date col1 col2

ID 日期 col1 col2

1      01.01.2015   A      B
1      01.03.2015   C

In SAS I do it like below:

在 SAS 中,我这样做:

proc transpose data = data;
  by ID Date;
  var product
run;

In pandas I used following code.

在Pandas中,我使用了以下代码。

data_b = data_a.pivot(index = ['patnum','day'], values = ['drug']).add_prefix('p')

This is giving following error.

这是给出以下错误。

ValueError: cannot label index with a null key

Why I'm getting above error? How can I avoid it?

为什么我会出现上述错误?我怎样才能避免它?

I'm using latest version in Pandas.

我在 Pandas 中使用最新版本。

回答by piRSquared

use groupby

groupby

for listobjects

对于list对象

df.groupby(['ID', 'date'])['Product'].apply(list)

ID  date      
1   01.01.2015    [A, B]
    01.03.2015       [C]
Name: Product, dtype: object

for a dataframe

对于数据框

df.groupby(['ID', 'date'])['Product'].apply(list).apply(pd.Series)

               0    1
ID date              
1  01.01.2015  A    B
   01.03.2015  C  NaN

回答by Psidom

You need to create another column to identify rows within each date to help you pivot:

您需要创建另一列来标识每个日期内的行以帮助您pivot

df.assign(Count = df.groupby('date').cumcount()).pivot("date", "Count", "Product")

#    Count  0      1
#date       
#01.01.2015 A      B
#01.03.2015 C   None

回答by scatter

A year later I have the same problem, rearranging the code to the following solved it:

一年后我遇到了同样的问题,将代码重新排列为以下解决了它:

pivot_df = pd.pivot_table(df, index =['coulmn1','coulmn2'])

being new to Python, I dont know why this works, I also dont know if its good or bad coding and nor do I know the cause of error message we both got...

作为 Python 新手,我不知道为什么会这样,我也不知道它的编码是好​​是坏,我也不知道我们都收到的错误消息的原因......

回答by priyanshu

Please use DataFrame.pivot_table()instead of DataFrame.pivot()

请使用DataFrame.pivot_table()代替DataFrame.pivot()

I got the same error and corrected it using above mentioned correction.

我遇到了同样的错误并使用上述更正进行了更正。

Thanks.

谢谢。