- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在寻找如何为 pandas 交叉表排序列,但无济于事。我特别需要根据日期值对格式化日期 (mmm yy) 的列进行排序,而不是根据月份的 3 个字母名称 (mmm) 按字母顺序排序。
这是我的代码的详细信息:
Python 3.3
Pandas 0.12.0
f_dtflt
是一个 pandas 数据框。
f_dtflt.COLLECTION_DATE
是 dtype datetime64[ns]
我的交叉表语句是:
pd.crosstab(f_dtflt.EW_REGIONCOLLSITE, f_dtflt.COLLECTION_DATE.apply(lambda x: x.strftime("%b %y")), margins=True)
输出为:
COLLECTION_DATE Apr 13 Aug 13 Dec 12 Feb 13 Jan 13 Jul 13 Jun 13
EW_REGIONCOLLSITE
EAST 1964 2092 2280 2272 2757 2113 1902
WEST 2579 2011 1003 2351 2216 1506 1823
All 4543 4103 3283 4623 4973 3619 3725
COLLECTION_DATE Mar 13 May 13 Nov 12 Oct 12 Sep 13 All
EW_REGIONCOLLSITE
EAST 1682 1981 2108 825 975 22951
WEST 2770 3014 407 42 888 20610
All 4452 4995 2515 867 1863 43561
我希望这些列按升序日期排序...10 月 12 日、11 月 12 日、...1 月 13 日、...9 月 13 日。我认识到我可以将日期格式化为 yy-mm(例如 13-01),但这些标签将在报告中使用,这是我希望不要做出的妥协。
我是 python 和 pandas 的新手,所以请通过连接您的回答中的任何点来帮助新手!非常感谢。
<小时/>方法一
编辑回应@Andy 答案的第一部分。第 3 步有问题:
我已尝试实现 Andy 的建议,以下是有关此工作的更多信息。
1) 我运行了以下行来查看日期。以下行创建诸如“2012-10”之类的值作为收集日期。 (通过打印“美化”?)
print(pd.DatetimeIndex(f_dtflt['COLLECTION_DATE']).to_period('M'))
2) 当上述语句输入交叉表时,它会将月份值更改为数字,例如 513、514 等(字段中的实际值?)
table1=pd.crosstab(f_dtflt.EW_REGIONCOLLSITE, pd.DatetimeIndex(f_dtflt['COLLECTION_DATE']).to_period('M'), margins=True)
这是输出:
col_0 513 514 515 516 517 518 519 520 521 522
EW_REGIONCOLLSITE
EAST 825 2108 2280 2757 2272 1682 1964 1981 1902 2113
WEST 42 407 1003 2216 2351 2770 2579 3014 1823 1506
All 867 2515 3283 4973 4623 4452 4543 4995 3725 3619
col_0 523 524 All
EW_REGIONCOLLSITE
EAST 2092 975 22951
WEST 2011 888 20610
All 4103 1863 43561
3) 当我运行以下代码时,它会抛出一个错误,指出“int”对象没有属性“strftime”
table1.columns = table1.columns.map(lambda x: x.strftime("%b %y"))
我对此进行了相当多的研究,以下是我的一些笔记:
# This runs and creates an array of strings: '513' etc.
pd.to_datetime(table1.columns.map(str), unit='M')
# The last entry in table1.columns is "All" and needs to be removed. Hence [:-1] slice.
# This also runs but seems to give years in 1630's.
pd.DatetimeIndex(table1.columns[:-1].map(str)).to_datetime('M')
# This does not run because it says object is immutable
table1.columns[:-1]=pd.DatetimeIndex(table1.columns[:-1].map(str)).to_datetime('M')
# This also runs but the output is weird. It seems to give an array of both dates and -1
table1.columns.reindex(pd.DatetimeIndex(table1.columns[:-1].map(str)).to_datetime('M'))
# Does not run: DatetimeIndex() must be called with a collection of some kind, '513' was passed
table1.columns = table1.columns.map(lambda x: pd.DatetimeIndex(str(x)).strftime("%b %y"))
# Does not run: DatetimeIndex object is not callable
table1.rename(columns=pd.DatetimeIndex(table1.columns[:-1].map(str)).to_datetime('M'))
4)这确实适用于标记交叉表中的列:
table1.columns.name = 'COLLECTION_DATE'
<小时/>
方法2
@Andy 给出了第二个建议,我尝试了一下,但无法让它发挥作用。问题的很大一部分是我对 python、pandas 和 numpy 不熟悉。当我试图整理它时,我为自己做了笔记。以下是我的笔记:
# Working with a new concept
# This creates row titles of 12 10, 12 11, etc.
table1=pd.crosstab(f_dtflt.EW_REGIONCOLLSITE, f_dtflt.COLLECTION_DATE.apply(lambda x: x.strftime("%y %m")), margins=True)
# This throws an error that yb is not defined
table1.columns.map(lambda yb: "%s %s" % (y, b) for y, b in yb.split())
# Tried to simplify and see what happens. Runs and creates an array of lists such as [['12, '10'], ['12', '11']...]
table1.columns.map(lambda x: x.split())
# Trying a different approach. This creates a numpy array of datetimes.
tempholder=table1.columns[:-1].map(lambda x: datetime.datetime(year=int(x[0:2]), month=int(x[3:]), day=1))
# Noted that f_dtflt['COLLECTION_DATE'] was a dtype of datetime64[ns] but tempholder was dtype object. So had issue.
# Convert to datetime64
# Get error: Out of bounds nanosecond timestamp: 12-10-01 00:00:00
tempholder=pd.to_datetime(tempholder)
# Tempholder is an array of datetimes from the datetime module. I used the pandas date function above.
# Need to change that and use python datetime module function.
# Does not work: 'numpy.ndarray' object has no attribute 'apply'...
# this is a pandas function which does not work on a numpy array.
tempholder.apply(lambda x: x.strftime('%b %y'))
# This works for numpy array but I can't tell what it contains.
# print(tempholder) gives <map object at 0x0000000026C04F28>
# tempholder gives Out[169]: <builtins.map at 0x26c04f28>
tempholder=map(lambda x: x.strftime('%b %y'), tempholder)
最佳答案
我从一个稍微不同的角度解决了这个问题,并创建了一个函数,可以用作在 pandas 中对交叉表中的列进行排序的通用方法。它也可能适用于数据透视表,但我没有测试它,也没有查看细节。我想它也可以用于排序行标签,但我没有尝试这样做。
这将创建一个带有列标签的交叉表,例如“12 10_Oct 12”和 12 11_Nov 12”。该标签有效地强制交叉表的字母顺序对我有利。标签的字母顺序部分与“_”和我想要使用的标签。
table_1=pd.crosstab(f_dtflt.EW_REGIONCOLLSITE, f_dtflt.COLLECTION_DATE.apply(lambda x: x.strftime("%y %m_%b %y")), margins=True)
输出:
"COLLECTION_DATE 12 10_Oct 12 12 11_Nov 12 12 12_Dec 12 13 01_Jan 13
EW_REGIONCOLLSITE
EAST 825 2108 2280 2757
WEST 42 407 1003 2216
All 867 2515 3283 4973
COLLECTION_DATE 13 02_Feb 13 13 03_Mar 13 13 04_Apr 13 13 05_May 13
EW_REGIONCOLLSITE
EAST 2272 1682 1964 1981
WEST 2351 2770 2579 3014
All 4623 4452 4543 4995
COLLECTION_DATE 13 06_Jun 13 13 07_Jul 13 13 08_Aug 13 13 09_Sep 13
EW_REGIONCOLLSITE
EAST 1902 2113 2092 975
WEST 1823 1506 2011 888
All 3725 3619 4103 1863
COLLECTION_DATE All
EW_REGIONCOLLSITE
EAST 22951
WEST 20610
All 43561 "
函数和调用:
def clean_label(label_list, margins='False'):
''' This function takes the column index list from a crosstab (or pivot table?) in pandas and removes the
part of the label before and including the "_". This allows the user to order the columns manually by creating
an alphabetical index followed by "_" and then the label that they would like to use. For example, a label such as
['a_Positive', 'b_Negative'] will be converted to ['Positive', 'Negative']. Another example would be to order dates
in a table from ['12 10_Oct 12', '12 11_Nov 12'] to ['Oct 12', 'Nov 12']
margins = False if the crosstab was created without margins and therefore does not have an "All" at the end of the list
margins = True if the crosstab was created with margins and therefore has an "All" at the end of the list
'''
corrected_list=list()
# If one creates margins in pivot/crosstab, will get the last column of "All"
# This has to be removed from the following code or it will throw an error.
if margins:
convert_list = label_list[:-1]
else:
convert_list = label_list
for l in convert_list:
x,y=l.split('_')
corrected_list.append(y)
if margins:
corrected_list.append('Total') # Renames "All" to "Total"
return corrected_list
# Change the labels on the crosstab table
table_1.columns=clean_label(table_1.columns, margins=True)
# Change name of columns
table_1.columns.name = 'Month of Collection'
# Change name of rows
table_1.index.name = 'Region'
输出(决赛 table ):
"Month of Collection Oct 12 Nov 12 Dec 12 Jan 13 Feb 13 Mar 13 Apr 13
Region
EAST 825 2108 2280 2757 2272 1682 1964
WEST 42 407 1003 2216 2351 2770 2579
All 867 2515 3283 4973 4623 4452 4543
Month of Collection May 13 Jun 13 Jul 13 Aug 13 Sep 13 Total
Region
EAST 1981 1902 2113 2092 975 22951
WEST 3014 1823 1506 2011 888 20610
All 4995 3725 3619 4103 1863 43561 "
关于python - Pandas 交叉表 : Change Order of Columns That Are Named as Formatted Dates (mmm yy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19501824/
我想使用单个(交叉)编译器来编译不同 ARM 调用约定的代码:因为我总是想使用浮点和 NEON 指令,所以我只想选择硬浮点调用约定或软浮点(softfp)调用约定。 我的编译器默认为硬浮点,但它支持我
假设我正在构建一个依赖于两个库的 java 应用程序:A 和 B。A 和 B 都依赖于库 C。管理 A 和 B 使用相同版本的最佳方法是什么所以他们不冲突?我正在使用 Gradle。 最佳答案 从 G
我想在按钮的文本上添加图像。如果我将图像添加为按钮的背景,它就会添加到文本下方。预期结果作为图像添加。请帮忙 更新:我需要以编程方式执行此操作。 最佳答案 在 XML 中, * 在代码中
我已经开始使用 CSS3 制作动画了。 我尝试创建一个动画汉堡菜单,但结果有点难看。顶部和底部的条向右平移一点。所以旋转动画不是很流畅和正确。 这是结果 => 这是我的代码: /* HTML */
给定一个具有2条相交曲线的图像,如下图所示,我如何使用opencv或python检测和区分2条曲线? (所以我需要2条单独的曲线) 最佳答案 您可以扫描每一列,并从连接的零件中识别出簇。 伪算法: l
我正在尝试在 redhat 集群(x86_64 主机)上设置 cross-mingw。我没有 root 访问权限,并且可用的 mingw 二进制文件不起作用(坏 glibc 版本等)。我正在阅读本教程
我正在尝试在javaFX中开发一个游戏,当两个图像相交时,分数将被更新,并且障碍物将不可见。但不幸的是,在游戏中分数不断更新。 我想我无法在游戏中正确地使图像不可见。 以下是相关类的完整代码: pac
pikastar dot com 是网站,当向下滚动它然后在导航菜单展开固定位置时它 > 将穿过主 div。我该如何修复它。 #topNav.sticky { box-shadow: 0 10
我正在使用 Eclipse为 ARM 处理器交叉编译 g++ 项目。我在 Windows 环境中使用 yagarto 工具链。我对 C 项目没有问题,但是对于 C++,我一直收到错误: libc.a(
我想从两个哈希数组中获取并集/交集/差集,例如: array1 = [{:name =>'Guy1', :age => 45},{:name =>'Guy2', :age => 45}] array2
有没有办法在调用任何 Controller 操作之前执行一些代码? 我需要根据 get 参数的值设置 session 变量,而不考虑调用哪个 Controller 。 当然,一旦这个处理完成,请求需要
我刚开始使用 3D 网格,面向用于有限元分析。我想在立方体状矩阵中模拟 Material 的夹杂物(任何形状,但主要对球体和椭圆体感兴趣)。这些夹杂物不应彼此重合。 所以我想为python使用某种包,
我想知道以跨平台方式操作应用程序设置的最佳解决方案是什么。 在 iOS 中,我们可以在设置屏幕中更改应用程序外部的设置,但在 windows phone 和 android 中我们没有。 所以,我的想
var barcodeNum = ko.observable(""); VelocityMeetings.scan = function (params) { var errorMessage = k
这个问题在这里已经有了答案: Transforming data.frame in R (2 个答案) 关闭10 年前。 过去我问过一个关于如何create cross tables from a
我有两个共享同一个工厂的 Controller 。其中一个 Controller 正在更新工厂变量。其他人应该注意该变化并稍后显示。 我是这样尝试的: http://plnkr.co/edit/q1N
标题不好,但这是我发现的将我的问题与简单的表格交叉区分开来的方式,因为我之前的研究总是让我接触到这类主题。 我有几个表 - 为了简化起见,我们只用 3 个表来命名它们:A、B、C。我想将它们全部放在一
我需要做这样的事情(在 MySQL 中),我使用 UNION 的尝试直到现在才奏效。 理论上: SELECT * FROM tableA A JOIN tableB B ON A.tableAId =
注意:使用SDL 2.0,Cross header class问题 我在类之间进行交叉引用,主要是我的类初始化渲染器和我的纹理类引用渲染初始化。现在,我已经能够运行该程序,直到我开始放入纹理类,代码也
我有一个这样的字母数组 var letters = ["Y", "X", "A", "Y", "O", "H", "A", "O", "O"]; 我创建了一个循环来
我是一名优秀的程序员,十分优秀!