- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 Python 的 Pandas 数据框中转换以下类型的样本数据。我遇到了其他几个讨论如何进行数据透视的 stackoverflow 答案:pivot_table No numeric types to aggregate
但是,当我使用 pivot_table()
时,我能够旋转数据。但是当我使用 set_index()
和 unstack()
时,出现以下错误:
AttributeError: 'NoneType' 对象没有属性 'unstack'
示例数据:
id responseTime label answers
ABC 2018-06-24 Category_1 [3]
ABC 2018-06-24 Category_2 [10]
ABC 2018-06-24 Category_3 [10]
DEF 2018-06-25 Category_1 [7]
DEF 2018-06-25 Category_8 [10]
GHI 2018-06-28 Category_3 [7]
期望的输出:
id responseTime category_1 category_2 category_3 category_8
ABC 2018-06-24 [3] [10] [10] NULL
DEF 2018-06-25 [7] NULL NULL [10]
GHI 2018-06-28 NULL NULL [7] NULL
这个有效:
df=pdDF.pivot_table(index=['items_id','responseTime'], columns='label', values='answers', aggfunc='first')
这行不通:
pdDF.set_index(['items_id','responseTime','label'], append=True, inplace=True).unstack('label')
我还使用了 pdDF[pdDF.isnull().any(axis=1)]
来确保答案列中没有任何 NULL 数据。我也使用了 append=False
但同样的错误发生了。
从其他线程来看,set_index()
和 unstack()
似乎比 pivot_table()
更有效。我也不想使用 pivot_table()
,因为它需要聚合函数,而我的答案列不包含数字数据。我不想使用默认值 (mean()
),所以我最终使用了 first()
。关于为什么一种方法有效而另一种方法无效的任何见解?
最佳答案
AttributeError: 'NoneType' object has no attribute 'unstack'
当您在 set_index
中使用 inplace = True
时,它就地修改了数据框。它不返回任何内容(None)
。所以你不能在 None
对象上使用 unstack
。
inplace : boolean, default False
Modify the DataFrame in place (do not create a new object)
使用:
df1 = pdDF.set_index(['items_id','responseTime','label']).unstack('label')
print(df1)
# Output:
id responseTime category_1 category_2 category_3 category_8
ABC 2018-06-24 [3] [10] [10] NULL
DEF 2018-06-25 [7] NULL NULL [10]
GHI 2018-06-28 NULL NULL [7] NULL
关于Python pandas dataframe pivot 仅适用于 pivot_table() 但不适用于 set_index() 和 unstack(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52230219/
设置: np.random.seed(0) iix = pd.MultiIndex.from_product([['bar', 'baz', 'foo'],['one', 'two']]) df =
我有一个这样开头的 CSV 文件: Year,Boys,Girls 1996,333490,315995 1997,329577,313518 1998,325903,309998 当我将它读入 pa
我在 Julia 数据帧的 python 中寻找类似 .set_index() 的函数。 我搜索并发现 NamedArray 可以给出与 Python 中的 .set_index() 类似的结果,如下
我正在尝试从我的数据框中的列之一设置数据框的索引。这个数据框的旧索引本质上是没有意义的。 但是当我使用 set_index(['Name']) 时,我添加了一个新列,这不是我想要的行为。我找不到解决方
我有一个数据帧数据。分组并重置索引后,我无法将日期列设置为索引。 data = data.groupby('Payment Date ') data['Payment Amount '].sum().
我有一个从 HDFS 上的 parquet 文件创建的 dask 数据框。使用 api: set_index 创 build 置索引时,它失败并出现以下错误。 File "/ebs/d1/agent/
我找不到在 Pandas 0.14 中查找多重索引的方法。这是我遇到问题的一些模拟数据。 代码: row1 = ['red', 'ferrari', 'mine'] row2 = ['blue', '
我有一个看起来像这样的数据框(索引未显示) Time Letter Type Value 0 A x 10 0 B y
从上面,你可以看到我已经将索引设置为“index”。我的期望是能够使用“索引”列来删除行,并且仅使用“Barangay”列作为功能而不是数据框的索引。 如上所示,仍然使用“Barangay”列作为引用
我想用 df.set_index() 函数更改我的 DataFrame 索引列。虽然这提供了一个功能解决方案,但它创建了一个我想摆脱的“额外”行。 df = pd.DataFrame({'A': ['
我有很多大约这种类型的 DataFrame: import pandas as pd import numpy as np x1 = pd.DataFrame(np.vstack((np.random
我有一个如下所示的数据框: Priority RID_solve Prob RID_prob Remarks 0 1 5001 34
我有以下数据框: df = pd.DataFrame({ 'Trader': 'Carl Mark Carl Joe Joe Carl Joe Carl'.split(), 'Product': li
我在我的数据框上运行以下代码函数: del dfname["Unnamed: 0"] dfname["date"] = pd.to_datetime(dfname["date"]) dfname.se
在 Pandas 中,如果我有一个如下所示的 DataFrame: 0 1 2 3 4 5 6 0
我有一个数据框,正在尝试将索引设置为“时间戳”列。目前索引只是一个行号。时间戳格式的一个例子是:2015-09-03 16:35:00 我试过设置索引: df.set_index('Timestamp
假设我创建了一个带有两列的 pandas DataFrame,b(一个 DateTime)和 c(一个整数)。现在我想从第一列 (b) 中的值创建一个 DatetimeIndex: import pa
我有一个看起来像 like this 的数据框: 我想将 'TIME_STAMP_NEW' 列作为索引。当前代码: twoweektable['TIME_STAMP_NEW'] = pd.to_dat
data = [['g1','a',1],['g1','b',2],['g2','b',3],['g2','a',4]] df = pandas.DataFrame(data=data, column
我正在尝试调用 df.set_index,使我设置索引的列的 dtype 是新的 index.dtype。不幸的是,在下面的示例中,set_index 更改了 dtype。 df = pd.DataF
我是一名优秀的程序员,十分优秀!