- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
最初我有 DF,其中 1 列操作用 DatetimeIndex 索引:
In [371]: dates
2013-12-29 19:21:00 action1
2013-12-29 19:21:01 action2
2013-12-29 19:21:11 action1
2013-12-29 19:21:13 action2
...
In [372]: dates.index
Out[372]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-12-29 19:02:27, ..., 2014-01-13 16:30:31]
Length: 108957, Freq: None, Timezone: None
我想绘制某种类型的 Action 数量与一天的对比
因此我使用 agg
将操作按日期分组
grouped = dates.groupby([dates.index.to_period(freq = 'D'), 'actiontype']).agg(len)
这给了我多索引系列:
...
2014-01-13 action1 435
action2 2067
..
2014-01-14 action1 455
action2 1007
...
这似乎正是我所需要的。
但是当尝试unstack
系列以摆脱 MultiIndex 并绘制我的数据时,出现了错误:
In [379]: grouped.unstack()
ValueError: freq not specified and cannot be inferred from first element
我这里有什么错误?谢谢。
最佳答案
如果您需要使用 .unstack()
但它不适用于该多索引,则从非索引数据开始
index mydate action
0 2000-12-29 00:10:00 action1
1 2000-12-29 00:20:00 action2
2 2000-12-29 00:30:00 action2
3 2000-12-29 00:40:00 action1
4 2000-12-29 00:50:00 action1
5 2000-12-31 00:10:00 action1
6 2000-12-31 00:20:00 action2
7 2000-12-31 00:30:00 action2
你可以做类似的事情
df['day'] = df['mydate'].apply(lambda x: x.split()[0])
counts = df.groupby(['day', 'action']).agg(len)
基本上你忘记了日期时间是一个日期时间,你只是把它作为一个字符串,你只保留日期,丢弃时间。现在 pandas 在时间维度上是愚蠢的,但是 counts.unstack()
给你
mydate
action action1 action2
day
2000-12-29 3 2
2000-12-31 1 2
关于python - Pandas unstack 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21352520/
尝试将 pandas DataFrames 从宽格式转换为长格式。 我尝试过 melt() ,使用wide_to_long() (简单的 melt()),但一直与我收到的语法和输出混淆。 我还阅读了
我试过 pivot和 groupby + unstack ,两者都给了我错误。 错误说 "Unstacked DataFrame is too big, causing int32 overflow"
我有一个如下所示的数据框: import pandas as pd df = pd.DataFrame({'type_a': [1,0,0,0,0,1,0,0,0,1],
我正在尝试解开张量,因为我需要一个序列作为 RNN 的输入。我正在使用可变序列长度,这使我无法正确使用 tf.unstack . def MapToSequences(x): # x.get_
我正在尝试通过取消嵌套/取消堆叠 df 列,将包含 value 列、两个日期列( start 和 end )和间隔列( duration )的数据帧 duration 转换为长格式。 library(
我有一个如下所示的数据框 op1 = pd.DataFrame({ 'subject_id':[1,1,1,1,2,2,2,2], 'date' : ['1/1/2017','1/2/2017','1
最初我有 DF,其中 1 列操作用 DatetimeIndex 索引: In [371]: dates 2013-12-29 19:21:00 action1 2013-12-29 19:21:
我在问自己是否有可能取消堆叠多索引数据帧的一层,以便不对返回的数据帧的其余索引进行排序!代码示例: arrays = [["room1", "room1", "room1", "room1", "ro
似乎有很多这方面的内容,但我找不到我需要的东西。我正在使用 unstack() 创建一个可以绘制其中项目的 DataFrame。 启动 DataFrame 示例: Date word
我有一个如下所示的数据框: 名称 值 1 值 2 A 100 101 A 100 102 A 100 103 B 200 201 B 200 202 B 200 203 C 300 301 C 30
有np.stack在 NumPy ,但是否有相反的np.unstack同 tf.unstack ? 最佳答案 遇到这么晚,这里有一个简单得多的答案: def unstack(a, axis=0):
我正在开发一个包含混合类型值(timedeltas 和 int)的 MultiIndex 系列: char 7 a 103 minutes s 6
我使用 groupby 和 sum 创建了以下数据框:- year_month Country 2008-01 Afghanistan 2
我编写了以下函数将数据框的几列转换为数值: def factorizeMany(data, columns): """ Factorize a bunch of columns in a da
我正在尝试拆开两列: cols = res.columns[:31] res[cols] = res[cols].ffill() res = res.set_index(cols + [31])[32
我有这个 Pandas DataFrame: rnd non-rnd first last andrew wood 0 1
这个问题已经有答案了: How to unnest (explode) a column in a pandas DataFrame, into multiple rows (16 个回答) 已关闭
我有一个名为 afplot 的数据框: apple_fplot = apple_f1.groupby(['Year','Domain Category'])['Value'].sum() afplot
我正在尝试将 LSTM 与具有不同时间步长(不同帧数)的输入一起使用。 rnn.static_rnn 的输入应该是一个 tf 序列(不是 tf!)。所以,我应该将我的输入转换为序列。我尝试使用 tf.
我遇到了以下对行和列标题进行排序的问题。 这里是重现这个的方法: X =pd.DataFrame(dict(x=np.random.normal(size=100), y=np.random.norm
我是一名优秀的程序员,十分优秀!