2-7_Pandas_Timestamps时间戳

# * coding:utf-8_*_
# 作者     :XiangLin
# 创建时间 :12/02/2020 19:06
# 文件     :2-7_Pandas_Timestamps.py
# IDE      :PyCharm
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
pd.set_option('display.max_columns',1000)
pd.set_option('display.width', 1000)
pd.set_option('display.max_colwidth',1000)

7.1 Unix时间戳怎么搞?

读数据,给定列名

# Read it, and remove the last row
popcon = pd.read_csv('popularity-contest',sep=' ',)[:-1]
popcon.columns = ['atime', 'ctime', 'package-name', 'mru-program', 'tag']
print(popcon.head())
输出:
        atime       ctime  package-name                                   mru-program             tag
0  1387295797  1367633260     perl-base                                 /usr/bin/perl             NaN
1  1387295796  1354370480         login                                       /bin/su             NaN
2  1387295743  1354341275    libtalloc2  /usr/lib/x86_64-linux-gnu/libtalloc.so.2.0.7             NaN
3  1387295743  1387224204  libwbclient0    /usr/lib/x86_64-linux-gnu/libwbclient.so.0  <RECENT-CTIME>
4  1387295742  1354341253   libselinux1         /lib/x86_64-linux-gnu/libselinux.so.1             NaN

前两列分别是改动时间和创建时间

开始的时候我们需要转成int型

popcon['atime'] = popcon['atime'].astype(int)
popcon['ctime'] = popcon['ctime'].astype(int)
# 然后可以用 pd.to_datetime 函数去把整型按照时间戳转成具体的日期和时间。
popcon['atime'] = pd.to_datetime(popcon['atime'],unit='s')
popcon['ctime'] = pd.to_datetime(popcon['ctime'],unit='s')
print(popcon['atime'].dtype)
# 你再看看 atime 和 ctime 字段,就变成标准时间啦!
print(popcon[:5])
输出:
               atime               ctime  package-name                                   mru-program             tag
0 2013-12-17 15:56:37 2013-05-04 02:07:40     perl-base                                 /usr/bin/perl             NaN
1 2013-12-17 15:56:36 2012-12-01 14:01:20         login                                       /bin/su             NaN
2 2013-12-17 15:55:43 2012-12-01 05:54:35    libtalloc2  /usr/lib/x86_64-linux-gnu/libtalloc.so.2.0.7             NaN
3 2013-12-17 15:55:43 2013-12-16 20:03:24  libwbclient0    /usr/lib/x86_64-linux-gnu/libwbclient.so.0  <RECENT-CTIME>
4 2013-12-17 15:55:42 2012-12-01 05:54:13   libselinux1         /lib/x86_64-linux-gnu/libselinux.so.1             NaN

7.2 根据时间筛选数据

test = popcon[popcon['atime'] > '2013-12-17']
# 这个操作是帮你复习一下字符串的操作
nonlibraries = popcon[~popcon['package-name'].str.contains('lib')]
print(nonlibraries.head())
# 排个序
sort_non = nonlibraries.sort_values('ctime',ascending=False)
print(sort_non)
输出:
               atime               ctime  package-name                                   mru-program             tag
0 2013-12-17 15:56:37 2013-05-04 02:07:40     perl-base                                 /usr/bin/perl             NaN
1 2013-12-17 15:56:36 2012-12-01 14:01:20         login                                       /bin/su             NaN
2 2013-12-17 15:55:43 2012-12-01 05:54:35    libtalloc2  /usr/lib/x86_64-linux-gnu/libtalloc.so.2.0.7             NaN
3 2013-12-17 15:55:43 2013-12-16 20:03:24  libwbclient0    /usr/lib/x86_64-linux-gnu/libwbclient.so.0  <RECENT-CTIME>
4 2013-12-17 15:55:42 2012-12-01 05:54:13   libselinux1         /lib/x86_64-linux-gnu/libselinux.so.1             NaN
                 atime               ctime        package-name                           mru-program  tag
0  2013-12-17 15:56:37 2013-05-04 02:07:40           perl-base                         /usr/bin/perl  NaN
1  2013-12-17 15:56:36 2012-12-01 14:01:20               login                               /bin/su  NaN
17 2013-12-17 15:55:33 2013-11-25 16:25:38     fingerprint-gui  /lib/security/pam_fingerprint-gui.so  NaN
18 2013-12-17 15:55:32 2012-12-01 05:53:57                dash                             /bin/dash  NaN
19 2013-12-17 15:55:29 2012-12-01 05:54:37  popularity-contest          /usr/sbin/popularity-contest  NaN
                   atime               ctime                    package-name                                                                        mru-program             tag
57   2013-12-17 04:55:39 2013-12-17 04:55:42                             ddd                                                                       /usr/bin/ddd  <RECENT-CTIME>
450  2013-12-16 20:03:20 2013-12-16 20:05:13                          nodejs                                                                       /usr/bin/npm  <RECENT-CTIME>
445  2013-12-16 20:03:20 2013-12-16 20:05:04           thunderbird-locale-en  /usr/lib/thunderbird-addons/extensions/langpack-en-GB@thunderbird.mozilla.org.xpi  <RECENT-CTIME>
454  2013-12-16 20:03:20 2013-12-16 20:05:04       switchboard-plug-keyboard                                       /usr/lib/plugs/pantheon/keyboard/options.txt  <RECENT-CTIME>
396  2013-12-16 20:08:27 2013-12-16 20:05:03                 software-center                                                   /usr/sbin/update-software-center  <RECENT-CTIME>
449  2013-12-16 20:03:20 2013-12-16 20:05:00                samba-common-bin                                                                /usr/bin/net.samba3  <RECENT-CTIME>
397  2013-12-16 20:08:25 2013-12-16 20:04:59           postgresql-client-9.1                                                   /usr/lib/postgresql/9.1/bin/psql  <RECENT-CTIME>
398  2013-12-16 20:08:23 2013-12-16 20:04:58                  postgresql-9.1                                             /usr/lib/postgresql/9.1/bin/postmaster  <RECENT-CTIME>
452  2013-12-16 20:03:20 2013-12-16 20:04:55                        php5-dev                                                  /usr/include/php5/main/snprintf.h  <RECENT-CTIME>
440  2013-12-16 20:03:20 2013-12-16 20:04:54                        php-pear                                                        /usr/share/php/XML/Util.php  <RECENT-CTIME>
391  2013-12-16 20:09:01 2013-12-16 20:04:53                     php5-common                                                      /usr/lib/php5/20090626/pdo.so  <RECENT-CTIME>
390  2013-12-16 20:09:01 2013-12-16 20:04:52                      php5-mysql                                                    /usr/lib/php5/20090626/mysql.so  <RECENT-CTIME>
439  2013-12-16 20:03:20 2013-12-16 20:04:51                        php5-cgi                                                              /usr/lib/cgi-bin/php5  <RECENT-CTIME>
392  2013-12-16 20:09:01 2013-12-16 20:04:50                        php5-cli                                                                      /usr/bin/php5  <RECENT-CTIME>
400  2013-12-16 20:08:09 2013-12-16 20:04:37          linux-headers-3.2.0-57                           /usr/src/linux-headers-3.2.0-57/include/linux/vermagic.h  <RECENT-CTIME>
401  2013-12-16 20:08:09 2013-12-16 20:04:37  linux-headers-3.2.0-57-generic                   /usr/src/linux-headers-3.2.0-57-generic/include/linux/vermagic.h  <RECENT-CTIME>
446  2013-12-16 20:03:20 2013-12-16 20:04:26     landscape-client-ui-install                                               /usr/bin/landscape-client-ui-install  <RECENT-CTIME>
441  2013-12-16 20:03:20 2013-12-16 20:04:24            gnome-control-center                                                        /usr/bin/gnome-sound-applet  <RECENT-CTIME>
457  2013-12-16 20:03:20 2013-12-16 20:04:20               gir1.2-mutter-3.0                                                   /usr/lib/mutter/Meta-3.0.typelib  <RECENT-CTIME>
432  2013-12-16 20:06:01 2013-12-16 20:04:20           flashplugin-installer                                      /usr/lib/flashplugin-installer/install_plugin  <RECENT-CTIME>
438  2013-12-16 20:03:20 2013-12-16 20:04:19               firefox-locale-fr                   /usr/lib/firefox/distribution/searchplugins/locale/fr/google.xml  <RECENT-CTIME>
447  2013-12-16 20:03:20 2013-12-16 20:04:18               firefox-locale-en             /usr/lib/firefox/distribution/searchplugins/locale/en-GB/wikipedia.xml  <RECENT-CTIME>
386  2013-12-16 20:33:11 2013-12-16 20:04:16                         firefox                                      /usr/lib/firefox/browser/icons/mozicon128.png  <RECENT-CTIME>
383  2013-12-16 20:33:24 2013-12-16 20:04:12                          evince                      /usr/lib/nautilus/extensions-3.0/libevince-properties-page.so  <RECENT-CTIME>
459  2013-12-16 20:03:20 2013-12-16 20:04:08                      contractor                                                                /usr/bin/contractor  <RECENT-CTIME>
442  2013-12-16 20:03:20 2013-12-16 20:04:07                           julia                                                                     /usr/bin/julia  <RECENT-CTIME>
405  2013-12-16 20:06:53 2013-12-16 20:04:05                 virtualbox-dkms                              /usr/src/virtualbox-4.3.2/include/version-generated.h  <RECENT-CTIME>
444  2013-12-16 20:03:20 2013-12-16 20:03:50                   virtualbox-qt                                                                /usr/bin/VirtualBox  <RECENT-CTIME>
407  2013-12-16 20:06:51 2013-12-16 20:03:50                      virtualbox                   /usr/lib/python2.7/dist-packages/vboxapi/VirtualBox_constants.py  <RECENT-CTIME>
455  2013-12-16 20:03:20 2013-12-16 20:03:45              slingshot-launcher                                                        /usr/bin/slingshot-launcher  <RECENT-CTIME>
...                  ...                 ...                             ...                                                                                ...             ...
2304 1970-01-01 00:00:00 1970-01-01 00:00:00                 example-content                                                                          <NOFILES>             NaN
2300 1970-01-01 00:00:00 1970-01-01 00:00:00   gstreamer1.0-plugins-base-doc                                                                          <NOFILES>             NaN
2297 1970-01-01 00:00:00 1970-01-01 00:00:00          thunderbird-globalmenu                                                                          <NOFILES>             NaN
2296 1970-01-01 00:00:00 1970-01-01 00:00:00                     pidgin-data                                                                          <NOFILES>             NaN
2295 1970-01-01 00:00:00 1970-01-01 00:00:00                       kate-data                                                                          <NOFILES>             NaN
2294 1970-01-01 00:00:00 1970-01-01 00:00:00                 fonts-tlwg-typo                                                                          <NOFILES>             NaN
2292 1970-01-01 00:00:00 1970-01-01 00:00:00                   gconf-service                                                                          <NOFILES>             NaN
2290 1970-01-01 00:00:00 1970-01-01 00:00:00                       gimp-data                                                                          <NOFILES>             NaN
2288 1970-01-01 00:00:00 1970-01-01 00:00:00               ttf-punjabi-fonts                                                                          <NOFILES>             NaN
2286 1970-01-01 00:00:00 1970-01-01 00:00:00                  python-gobject                                                                          <NOFILES>             NaN
2285 1970-01-01 00:00:00 1970-01-01 00:00:00                  tumbler-common                                                                          <NOFILES>             NaN
2318 1970-01-01 00:00:00 1970-01-01 00:00:00                   nautilus-data                                                                          <NOFILES>             NaN
2321 1970-01-01 00:00:00 1970-01-01 00:00:00                  xorg-docs-core                                                                          <NOFILES>             NaN
2324 1970-01-01 00:00:00 1970-01-01 00:00:00               fonts-tlwg-typist                                                                          <NOFILES>             NaN
2350 1970-01-01 00:00:00 1970-01-01 00:00:00                      cli-common                                                                          <NOFILES>             NaN
2384 1970-01-01 00:00:00 1970-01-01 00:00:00             fonts-tlwg-sawasdee                                                                          <NOFILES>             NaN
2383 1970-01-01 00:00:00 1970-01-01 00:00:00                    totem-common                                                                          <NOFILES>             NaN
2379 1970-01-01 00:00:00 1970-01-01 00:00:00             gtk2-engines-oxygen                                                                          <NOFILES>             NaN
2373 1970-01-01 00:00:00 1970-01-01 00:00:00  mobile-broadband-provider-info                                                                          <NOFILES>             NaN
2363 1970-01-01 00:00:00 1970-01-01 00:00:00                  brasero-common                                                                          <NOFILES>             NaN
2359 1970-01-01 00:00:00 1970-01-01 00:00:00                   xfonts-mathml                                                                          <NOFILES>             NaN
2348 1970-01-01 00:00:00 1970-01-01 00:00:00                    hyphen-en-us                                                                          <NOFILES>             NaN
2326 1970-01-01 00:00:00 1970-01-01 00:00:00                 heroku-toolbelt                                                                          <NOFILES>             NaN
2346 1970-01-01 00:00:00 1970-01-01 00:00:00       thunderbird-gnome-support                                                                          <NOFILES>             NaN
2345 1970-01-01 00:00:00 1970-01-01 00:00:00                 ttf-inconsolata                                                                          <NOFILES>             NaN
2340 1970-01-01 00:00:00 1970-01-01 00:00:00                        unity-2d                                                                          <NOFILES>             NaN
2335 1970-01-01 00:00:00 1970-01-01 00:00:00                   wine-gecko1.4                                                                          <NOFILES>             NaN
2333 1970-01-01 00:00:00 1970-01-01 00:00:00        gwibber-service-facebook                                                                          <NOFILES>             NaN
2330 1970-01-01 00:00:00 1970-01-01 00:00:00                  ubuntu-artwork                                                                          <NOFILES>             NaN
2894 1970-01-01 00:00:00 1970-01-01 00:00:00               python-apt-common                                                                          <NOFILES>             NaN

[1515 rows x 5 columns]

数据链接:链接:https://pan.baidu.com/s/1ri87d6RDOWQwXuPIeNiBig
提取码:znep
来自七月在线数据挖掘算法
向林
2020年2月13日于重庆城口
好好学习,天天向上,终有所获

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值