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']
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')
popcon['atime'].dtype
dtype('<M8[ns]')
你再看看 atime
和 ctime
字段,就变成标准时间啦!
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 |
6.2 根据时间筛选数据
另外一个操作是,我们需要按照时间去筛选数据,这个也很简单,你只需要告诉pandas,我需要大于/小于某个日期的数据!!
popcon = popcon[popcon['atime'] > '2013-12-17']
popcon.head()
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 |
test = popcon[popcon['atime'] > '2013-12-17']
这个操作是帮你复习一下字符串的操作
nonlibraries = popcon[~popcon['package-name'].str.contains('lib')]
nonlibraries.head()
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 |
排个序
nonlibraries.sort_values('ctime', ascending=False)
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> |
168 | 2013-12-17 00:06:25 | 2013-12-10 16:25:46 | pm-utils | /usr/lib/pm-utils/sleep.d/94cpufreq | NaN |
106 | 2013-12-17 04:28:03 | 2013-12-10 16:25:09 | ruby1.9.1 | /usr/bin/ruby1.9.1 | NaN |
107 | 2013-12-17 04:28:03 | 2013-12-10 16:25:09 | ruby1.9.3 | /usr/bin/ruby1.9.3 | NaN |
91 | 2013-12-17 04:43:36 | 2013-12-10 16:24:57 | git | /usr/lib/git-core/git-prune | NaN |
66 | 2013-12-17 04:55:38 | 2013-12-10 16:24:47 | apt-utils | /usr/bin/apt-extracttemplates | NaN |
29 | 2013-12-17 15:52:49 | 2013-12-10 16:22:37 | apt | /usr/bin/apt-config | NaN |
170 | 2013-12-17 00:06:25 | 2013-11-26 19:22:39 | bridge-utils | /lib/bridge-utils/ifupdown.sh | NaN |
94 | 2013-12-17 04:34:03 | 2013-11-25 22:23:32 | google-chrome-stable | /usr/bin/google-chrome-stable | NaN |
17 | 2013-12-17 15:55:33 | 2013-11-25 16:25:38 | fingerprint-gui | /lib/security/pam_fingerprint-gui.so | NaN |
74 | 2013-12-17 04:44:40 | 2013-11-25 16:19:55 | python-lazr.restfulclient | /usr/lib/python2.7/dist-packages/lazr.restfulclient-0.12.0-nspkg.pth | NaN |
171 | 2013-12-17 00:06:25 | 2013-11-25 16:19:08 | pulseaudio | /usr/lib/pulse-1.1/modules/libprotocol-cli.so | NaN |
172 | 2013-12-17 00:06:25 | 2013-11-25 16:19:06 | pulseaudio-utils | /usr/bin/pacmd | NaN |
87 | 2013-12-17 04:44:40 | 2013-11-25 16:18:31 | python-ubuntuone-client | /usr/lib/python2.7/dist-packages/ubuntuone-client.pth | NaN |
174 | 2013-12-17 00:06:25 | 2013-11-25 16:16:19 | procps | /sbin/sysctl | NaN |
120 | 2013-12-17 04:07:16 | 2013-11-25 16:16:16 | ifupdown | /sbin/ifup | NaN |
79 | 2013-12-17 04:44:40 | 2013-11-25 16:16:13 | lsb-release | /usr/bin/lsb_release | NaN |
165 | 2013-12-17 00:06:25 | 2013-11-25 16:14:24 | network-manager | /usr/lib/NetworkManager/nm-dispatcher.action | NaN |
169 | 2013-12-17 00:06:25 | 2013-11-25 16:14:21 | wpasupplicant | /sbin/wpa_cli | NaN |
128 | 2013-12-17 04:07:13 | 2013-11-25 16:14:19 | isc-dhcp-client | /sbin/dhclient3 | NaN |
83 | 2013-12-17 04:44:40 | 2013-11-25 16:07:09 | python2.7-minimal | /usr/bin/python2.7 | NaN |
86 | 2013-12-17 04:44:40 | 2013-11-25 16:07:09 | python-minimal | /usr/bin/python | NaN |
21 | 2013-12-17 15:53:05 | 2013-11-25 16:00:20 | dpkg | /usr/sbin/dpkg-statoverride | NaN |
77 | 2013-12-17 04:44:40 | 2013-11-04 21:08:32 | python-support | /usr/lib/python2.7/dist-packages/python-support.pth | NaN |
177 | 2013-12-17 00:06:24 | 2013-10-25 21:49:49 | postfix | /usr/sbin/postconf | NaN |
127 | 2013-12-17 04:07:15 | 2013-08-03 20:47:41 | bind9-host | /usr/bin/host | NaN |
122 | 2013-12-17 04:07:16 | 2013-08-03 20:47:39 | udev | /lib/udev/hotplug.functions | NaN |
166 | 2013-12-17 00:06:25 | 2013-07-04 13:30:06 | dbus | /usr/bin/dbus-send | NaN |
25 | 2013-12-17 15:52:54 | 2013-06-12 00:23:26 | cups | /usr/lib/cups/notifier/dbus | NaN |
178 | 2013-12-17 00:06:24 | 2013-05-04 02:15:16 | linux-sound-base | /lib/linux-sound-base/noOSS.modprobe.conf | NaN |
... | ... | ... | ... | ... | ... |
27 | 2013-12-17 15:52:53 | 2012-12-01 05:54:37 | logrotate | /usr/sbin/logrotate | NaN |
162 | 2013-12-17 04:07:05 | 2012-12-01 05:54:36 | ibus-gtk | /usr/lib/x86_64-linux-gnu/gtk-2.0/2.10.0/immodules/im-ibus.so | NaN |
134 | 2013-12-17 04:07:06 | 2012-12-01 05:54:36 | qt-at-spi | /usr/lib/x86_64-linux-gnu/qt4/plugins/accessiblebridge/libqspiaccessiblebridge.so | NaN |
167 | 2013-12-17 00:06:25 | 2012-12-01 05:54:28 | anacron | /usr/lib/pm-utils/sleep.d/95anacron | NaN |
163 | 2013-12-17 04:07:04 | 2012-12-01 05:54:22 | gnome-screensaver | /usr/lib/gnome-screensaver/gnome-screensaver-dialog | NaN |
175 | 2013-12-17 00:06:24 | 2012-12-01 05:54:19 | avahi-daemon | /usr/lib/avahi/avahi-daemon-check-dns.sh | NaN |
96 | 2013-12-17 04:28:16 | 2012-12-01 05:54:19 | aspell-en | /usr/lib/aspell/american-wo_accents.alias | NaN |
173 | 2013-12-17 00:06:25 | 2012-12-01 05:54:19 | consolekit | /usr/lib/ConsoleKit/ck-collect-session-info | NaN |
113 | 2013-12-17 04:26:39 | 2012-12-01 05:54:17 | x11-xserver-utils | /usr/bin/xrdb | NaN |
20 | 2013-12-17 15:53:05 | 2012-12-01 05:54:16 | mlocate | /usr/bin/updatedb.mlocate | NaN |
68 | 2013-12-17 04:47:50 | 2012-12-01 05:54:16 | groff-base | /usr/bin/gtbl | NaN |
26 | 2013-12-17 15:52:53 | 2012-12-01 05:54:16 | findutils | /usr/bin/xargs | NaN |
90 | 2013-12-17 04:43:44 | 2012-12-01 05:54:15 | rsync | /usr/bin/rsync | NaN |
23 | 2013-12-17 15:53:05 | 2012-12-01 05:54:15 | util-linux | /usr/bin/ionice | NaN |
30 | 2013-12-17 15:52:49 | 2012-12-01 05:54:15 | debianutils | /usr/bin/savelog | NaN |
180 | 2013-12-17 00:06:23 | 2012-12-01 05:54:15 | mawk | /usr/bin/mawk | NaN |
119 | 2013-12-17 04:07:24 | 2012-12-01 05:54:15 | lockfile-progs | /usr/bin/lockfile-remove | NaN |
54 | 2013-12-17 04:55:44 | 2012-12-01 05:54:14 | install-info | /usr/bin/ginstall-info | NaN |
28 | 2013-12-17 15:52:49 | 2012-12-01 05:54:13 | diffutils | /usr/bin/cmp | NaN |
110 | 2013-12-17 04:26:39 | 2012-12-01 05:54:13 | cpp | /usr/bin/cpp | NaN |
109 | 2013-12-17 04:26:39 | 2012-12-01 05:54:13 | cpp-4.6 | /usr/bin/x86_64-linux-gnu-cpp-4.6 | NaN |
97 | 2013-12-17 04:28:15 | 2012-12-01 05:54:13 | aspell | /usr/bin/aspell | NaN |
32 | 2013-12-17 05:11:46 | 2012-12-01 05:54:09 | wireless-regdb | /lib/crda/regulatory.bin | NaN |
31 | 2013-12-17 05:11:46 | 2012-12-01 05:54:09 | crda | /lib/crda/pubkeys/benh@debian.org.key.pub.pem | NaN |
58 | 2013-12-17 04:55:39 | 2012-12-01 05:53:59 | tar | /bin/tar | NaN |
64 | 2013-12-17 04:55:38 | 2012-12-01 05:53:57 | gzip | /bin/gzip | NaN |
123 | 2013-12-17 04:07:16 | 2012-12-01 05:53:57 | iproute | /sbin/ip | NaN |
117 | 2013-12-17 04:11:11 | 2012-12-01 05:53:57 | less | /usr/bin/less | NaN |
176 | 2013-12-17 00:06:24 | 2012-12-01 05:53:57 | module-init-tools | /sbin/lsmod | NaN |
18 | 2013-12-17 15:55:32 | 2012-12-01 05:53:57 | dash | /bin/dash | NaN |
88 rows × 5 columns