pandas挑选数据ITeye - 牛牛娱乐

pandas挑选数据ITeye

2019-01-10 17:59:15 | 作者: 泽雨 | 标签: 挑选,数据,进行 | 浏览: 2638

div p div id="cnblogs_post_body" /p
p nbsp; h1 id="挑选列" 挑选列 /h1 nbsp; /p
p nbsp; p 依据列名来挑选某列的数据 /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" span import /span pandas span as /span pd /p
p span import /span numpy span as /span np /p
p dates span = /span pd.date_range( span amp;quot;2017-01-08 amp;quot; /span , periods span = /span span 6 /span ) /p
p data span = /span pd.DataFrame(np.arange( span 24 /span ).reshape( span 6 /span , span 4 /span ), index span = /span dates, columns span = /span [ span amp;quot;A amp;quot; /span , span amp;quot;B amp;quot; /span , span amp;quot;C amp;quot; /span , span amp;quot;D amp;quot; /span ]) /p
p span print /span ( span amp;quot;data: amp;quot; /span ) /p
p span print /span (data) /p
p span # 挑选A列数据 /span /p
p span print /span ( span amp;quot;A列数据: amp;quot; /span ) /p
p span print /span (data[ span amp;quot;A amp;quot; /span ]) /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 输出成果: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" data: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;A nbsp; nbsp;B nbsp; nbsp;C nbsp; nbsp;D /p
p span 2017-01-08 /span nbsp; nbsp; span 0 /span nbsp; nbsp; span 1 /span nbsp; nbsp; span 2 /span nbsp; nbsp; span 3 /span /p
p span 2017-01-09 /span nbsp; nbsp; span 4 /span nbsp; nbsp; span 5 /span nbsp; nbsp; span 6 /span nbsp; nbsp; span 7 /span /p
p span 2017-01-10 /span nbsp; nbsp; span 8 /span nbsp; nbsp; span 9 /span nbsp; span 10 /span nbsp; span 11 /span /p
p span 2017-01-11 /span nbsp; span 12 /span nbsp; span 13 /span nbsp; span 14 /span nbsp; span 15 /span /p
p span 2017-01-12 /span nbsp; span 16 /span nbsp; span 17 /span nbsp; span 18 /span nbsp; span 19 /span /p
p span 2017-01-13 /span nbsp; span 20 /span nbsp; span 21 /span nbsp; span 22 /span nbsp; span 23 /span /p
p A列数据: /p
p span 2017-01-08 /span nbsp; nbsp; nbsp; span 0 /span /p
p span 2017-01-09 /span nbsp; nbsp; nbsp; span 4 /span /p
p span 2017-01-10 /span nbsp; nbsp; nbsp; span 8 /span /p
p span 2017-01-11 /span nbsp; nbsp; span 12 /span /p
p span 2017-01-12 /span nbsp; nbsp; span 16 /span /p
p span 2017-01-13 /span nbsp; nbsp; span 20 /span /p
p Freq: D, Name: A, dtype: int32 /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 也能够用点符号来进行: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" span print /span (data.A) /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 上面的功用跟data[ amp;quot;A amp;quot;]相同。 /p nbsp; /p
p nbsp; h1 id="挑选某几行数据" 挑选某几行数据 /h1 nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" span import /span pandas span as /span pd /p
p span import /span numpy span as /span np /p
p dates span = /span pd.date_range( span amp;quot;2017-01-08 amp;quot; /span , periods span = /span span 6 /span ) /p
p data span = /span pd.DataFrame(np.arange( span 24 /span ).reshape( span 6 /span , span 4 /span ), index span = /span dates, columns span = /span [ span amp;quot;A amp;quot; /span , span amp;quot;B amp;quot; /span , span amp;quot;C amp;quot; /span , span amp;quot;D amp;quot; /span ]) /p
p span print /span ( span amp;quot;data: amp;quot; /span ) /p
p span print /span (data) /p
p nbsp; /p
p span print /span ( span amp;quot;挑选0至3行的数据: amp;quot; /span ) /p
p span print /span (data[ span 0 /span : span 3 /span ]) /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 输出为: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" data: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;A nbsp; nbsp;B nbsp; nbsp;C nbsp; nbsp;D /p
p span 2017-01-08 /span nbsp; nbsp; span 0 /span nbsp; nbsp; span 1 /span nbsp; nbsp; span 2 /span nbsp; nbsp; span 3 /span /p
p span 2017-01-09 /span nbsp; nbsp; span 4 /span nbsp; nbsp; span 5 /span nbsp; nbsp; span 6 /span nbsp; nbsp; span 7 /span /p
p span 2017-01-10 /span nbsp; nbsp; span 8 /span nbsp; nbsp; span 9 /span nbsp; span 10 /span nbsp; span 11 /span /p
p span 2017-01-11 /span nbsp; span 12 /span nbsp; span 13 /span nbsp; span 14 /span nbsp; span 15 /span /p
p span 2017-01-12 /span nbsp; span 16 /span nbsp; span 17 /span nbsp; span 18 /span nbsp; span 19 /span /p
p span 2017-01-13 /span nbsp; span 20 /span nbsp; span 21 /span nbsp; span 22 /span nbsp; span 23 /span /p
p 挑选 span 0 /span 至 span 3 /span 行的数据: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; A nbsp; B nbsp; nbsp;C nbsp; nbsp;D /p
p span 2017-01-08 /span nbsp; span 0 /span nbsp; span 1 /span nbsp; nbsp; span 2 /span nbsp; nbsp; span 3 /span /p
p span 2017-01-09 /span nbsp; span 4 /span nbsp; span 5 /span nbsp; nbsp; span 6 /span nbsp; nbsp; span 7 /span /p
p span 2017-01-10 /span nbsp; span 8 /span nbsp; span 9 /span nbsp; span 10 /span nbsp; span 11 /span /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 也能够依据索引号规模来挑选某几行的数据。 br / 比方,如下的比方中咱们就挑选出2017-01-10到2017-01-12的数据: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" span import /span pandas span as /span pd /p
p span import /span numpy span as /span np /p
p dates span = /span pd.date_range( span amp;quot;2017-01-08 amp;quot; /span , periods span = /span span 6 /span ) /p
p data span = /span pd.DataFrame(np.arange( span 24 /span ).reshape( span 6 /span , span 4 /span ), index span = /span dates, columns span = /span [ span amp;quot;A amp;quot; /span , span amp;quot;B amp;quot; /span , span amp;quot;C amp;quot; /span , span amp;quot;D amp;quot; /span ]) /p
p span print /span ( span amp;quot;data: amp;quot; /span ) /p
p span print /span (data) /p
p nbsp; /p
p span print /span ( span amp;quot;依照索引挑选数据: amp;quot; /span ) /p
p span print /span (data[ span amp;quot;2017-01-10 amp;quot; /span : span amp;quot;2017-01-12 amp;quot; /span ]) /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 输出为: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" data: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;A nbsp; nbsp;B nbsp; nbsp;C nbsp; nbsp;D /p
p span 2017-01-08 /span nbsp; nbsp; span 0 /span nbsp; nbsp; span 1 /span nbsp; nbsp; span 2 /span nbsp; nbsp; span 3 /span /p
p span 2017-01-09 /span nbsp; nbsp; span 4 /span nbsp; nbsp; span 5 /span nbsp; nbsp; span 6 /span nbsp; nbsp; span 7 /span /p
p span 2017-01-10 /span nbsp; nbsp; span 8 /span nbsp; nbsp; span 9 /span nbsp; span 10 /span nbsp; span 11 /span /p
p span 2017-01-11 /span nbsp; span 12 /span nbsp; span 13 /span nbsp; span 14 /span nbsp; span 15 /span /p
p span 2017-01-12 /span nbsp; span 16 /span nbsp; span 17 /span nbsp; span 18 /span nbsp; span 19 /span /p
p span 2017-01-13 /span nbsp; span 20 /span nbsp; span 21 /span nbsp; span 22 /span nbsp; span 23 /span /p
p 依照索引挑选数据: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;A nbsp; nbsp;B nbsp; nbsp;C nbsp; nbsp;D /p
p span 2017-01-10 /span nbsp; nbsp; span 8 /span nbsp; nbsp; span 9 /span nbsp; span 10 /span nbsp; span 11 /span /p
p span 2017-01-11 /span nbsp; span 12 /span nbsp; span 13 /span nbsp; span 14 /span nbsp; span 15 /span /p
p span 2017-01-12 /span nbsp; span 16 /span nbsp; span 17 /span nbsp; span 18 /span nbsp; span 19 /span /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; h1 id="运用loc进行挑选" 运用loc进行挑选 /h1 nbsp; /p
p nbsp; p 运用loc挑选某几行的数据: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" span import /span pandas span as /span pd /p
p span import /span numpy span as /span np /p
p dates span = /span pd.date_range( span amp;quot;2017-01-08 amp;quot; /span , periods span = /span span 6 /span ) /p
p data span = /span pd.DataFrame(np.arange( span 24 /span ).reshape( span 6 /span , span 4 /span ), index span = /span dates, columns span = /span [ span amp;quot;A amp;quot; /span , span amp;quot;B amp;quot; /span , span amp;quot;C amp;quot; /span , span amp;quot;D amp;quot; /span ]) /p
p span print /span ( span amp;quot;data: amp;quot; /span ) /p
p span print /span (data) /p
p nbsp; /p
p span print /span ( span amp;quot;依照索引挑选数据: amp;quot; /span ) /p
p span print /span (data.loc[ span amp;quot;2017-01-10 amp;quot; /span : span amp;quot;2017-01-12 amp;quot; /span ]) /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 输出: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" data: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;A nbsp; nbsp;B nbsp; nbsp;C nbsp; nbsp;D /p
p span 2017-01-08 /span nbsp; nbsp; span 0 /span nbsp; nbsp; span 1 /span nbsp; nbsp; span 2 /span nbsp; nbsp; span 3 /span /p
p span 2017-01-09 /span nbsp; nbsp; span 4 /span nbsp; nbsp; span 5 /span nbsp; nbsp; span 6 /span nbsp; nbsp; span 7 /span /p
p span 2017-01-10 /span nbsp; nbsp; span 8 /span nbsp; nbsp; span 9 /span nbsp; span 10 /span nbsp; span 11 /span /p
p span 2017-01-11 /span nbsp; span 12 /span nbsp; span 13 /span nbsp; span 14 /span nbsp; span 15 /span /p
p span 2017-01-12 /span nbsp; span 16 /span nbsp; span 17 /span nbsp; span 18 /span nbsp; span 19 /span /p
p span 2017-01-13 /span nbsp; span 20 /span nbsp; span 21 /span nbsp; span 22 /span nbsp; span 23 /span /p
p 依照索引挑选数据: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;A nbsp; nbsp;B nbsp; nbsp;C nbsp; nbsp;D /p
p span 2017-01-10 /span nbsp; nbsp; span 8 /span nbsp; nbsp; span 9 /span nbsp; span 10 /span nbsp; span 11 /span /p
p span 2017-01-11 /span nbsp; span 12 /span nbsp; span 13 /span nbsp; span 14 /span nbsp; span 15 /span /p
p span 2017-01-12 /span nbsp; span 16 /span nbsp; span 17 /span nbsp; span 18 /span nbsp; span 19 /span /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 也能够依照列进行挑选数据,比方,咱们想要挑选其间B和C列的数据: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" span import /span pandas span as /span pd /p
p span import /span numpy span as /span np /p
p dates span = /span pd.date_range( span amp;quot;2017-01-08 amp;quot; /span , periods span = /span span 6 /span ) /p
p data span = /span pd.DataFrame(np.arange( span 24 /span ).reshape( span 6 /span , span 4 /span ), index span = /span dates, columns span = /span [ span amp;quot;A amp;quot; /span , span amp;quot;B amp;quot; /span , span amp;quot;C amp;quot; /span , span amp;quot;D amp;quot; /span ]) /p
p span print /span ( span amp;quot;data: amp;quot; /span ) /p
p span print /span (data) /p
p nbsp; /p
p span print /span ( span amp;quot;挑选某两列的数据: amp;quot; /span ) /p
p span print /span (data.loc[:, [ span amp;quot;B amp;quot; /span , span amp;quot;C amp;quot; /span ]]) /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 输出为: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" data: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;A nbsp; nbsp;B nbsp; nbsp;C nbsp; nbsp;D /p
p span 2017-01-08 /span nbsp; nbsp; span 0 /span nbsp; nbsp; span 1 /span nbsp; nbsp; span 2 /span nbsp; nbsp; span 3 /span /p
p span 2017-01-09 /span nbsp; nbsp; span 4 /span nbsp; nbsp; span 5 /span nbsp; nbsp; span 6 /span nbsp; nbsp; span 7 /span /p
p span 2017-01-10 /span nbsp; nbsp; span 8 /span nbsp; nbsp; span 9 /span nbsp; span 10 /span nbsp; span 11 /span /p
p span 2017-01-11 /span nbsp; span 12 /span nbsp; span 13 /span nbsp; span 14 /span nbsp; span 15 /span /p
p span 2017-01-12 /span nbsp; span 16 /span nbsp; span 17 /span nbsp; span 18 /span nbsp; span 19 /span /p
p span 2017-01-13 /span nbsp; span 20 /span nbsp; span 21 /span nbsp; span 22 /span nbsp; span 23 /span /p
p 挑选某两列的数据: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;B nbsp; nbsp;C /p
p span 2017-01-08 /span nbsp; nbsp; span 1 /span nbsp; nbsp; span 2 /span /p
p span 2017-01-09 /span nbsp; nbsp; span 5 /span nbsp; nbsp; span 6 /span /p
p span 2017-01-10 /span nbsp; nbsp; span 9 /span nbsp; span 10 /span /p
p span 2017-01-11 /span nbsp; span 13 /span nbsp; span 14 /span /p
p span 2017-01-12 /span nbsp; span 17 /span nbsp; span 18 /span /p
p span 2017-01-13 /span nbsp; span 21 /span nbsp; span 22 /span /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 假如只想挑选某几行中某几列的数据,能够对上面的比方进行一下略微的修正就能完成: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" span import /span pandas span as /span pd /p
p span import /span numpy span as /span np /p
p dates span = /span pd.date_range( span amp;quot;2017-01-08 amp;quot; /span , periods span = /span span 6 /span ) /p
p data span = /span pd.DataFrame(np.arange( span 24 /span ).reshape( span 6 /span , span 4 /span ), index span = /span dates, columns span = /span [ span amp;quot;A amp;quot; /span , span amp;quot;B amp;quot; /span , span amp;quot;C amp;quot; /span , span amp;quot;D amp;quot; /span ]) /p
p span print /span ( span amp;quot;data: amp;quot; /span ) /p
p span print /span (data) /p
p nbsp; /p
p span print /span ( span amp;quot;挑选某几行某几列的数据: amp;quot; /span ) /p
p span print /span (data.loc[ span amp;quot;2017-01-09 amp;quot; /span : span amp;quot;2017-01-12 amp;quot; /span , [ span amp;quot;B amp;quot; /span , span amp;quot;C amp;quot; /span ]]) /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 输出为: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" data: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;A nbsp; nbsp;B nbsp; nbsp;C nbsp; nbsp;D /p
p span 2017-01-08 /span nbsp; nbsp; span 0 /span nbsp; nbsp; span 1 /span nbsp; nbsp; span 2 /span nbsp; nbsp; span 3 /span /p
p span 2017-01-09 /span nbsp; nbsp; span 4 /span nbsp; nbsp; span 5 /span nbsp; nbsp; span 6 /span nbsp; nbsp; span 7 /span /p
p span 2017-01-10 /span nbsp; nbsp; span 8 /span nbsp; nbsp; span 9 /span nbsp; span 10 /span nbsp; span 11 /span /p
p span 2017-01-11 /span nbsp; span 12 /span nbsp; span 13 /span nbsp; span 14 /span nbsp; span 15 /span /p
p span 2017-01-12 /span nbsp; span 16 /span nbsp; span 17 /span nbsp; span 18 /span nbsp; span 19 /span /p
p span 2017-01-13 /span nbsp; span 20 /span nbsp; span 21 /span nbsp; span 22 /span nbsp; span 23 /span /p
p 挑选某几行某几列的数据: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;B nbsp; nbsp;C /p
p span 2017-01-09 /span nbsp; nbsp; span 5 /span nbsp; nbsp; span 6 /span /p
p span 2017-01-10 /span nbsp; nbsp; span 9 /span nbsp; span 10 /span /p
p span 2017-01-11 /span nbsp; span 13 /span nbsp; span 14 /span /p
p span 2017-01-12 /span nbsp; span 17 /span nbsp; span 18 /span /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; h1 id="依据方位索引挑选数据" 依据方位索引挑选数据 /h1 nbsp; /p
p nbsp; p 方位索引的办法为iloc,例如,挑选第3行第2列的数据: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" span import /span pandas span as /span pd /p
p span import /span numpy span as /span np /p
p dates span = /span pd.date_range( span amp;quot;2017-01-08 amp;quot; /span , periods span = /span span 6 /span ) /p
p data span = /span pd.DataFrame(np.arange( span 24 /span ).reshape( span 6 /span , span 4 /span ), index span = /span dates, columns span = /span [ span amp;quot;A amp;quot; /span , span amp;quot;B amp;quot; /span , span amp;quot;C amp;quot; /span , span amp;quot;D amp;quot; /span ]) /p
p span print /span ( span amp;quot;data: amp;quot; /span ) /p
p span print /span (data) /p
p nbsp; /p
p span print /span ( span amp;quot;挑选第3行第2列的数据: amp;quot; /span ) /p
p span print /span (data.iloc[ span 3 /span , span 1 /span ]) /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 输出: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" data: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;A nbsp; nbsp;B nbsp; nbsp;C nbsp; nbsp;D /p
p span 2017-01-08 /span nbsp; nbsp; span 0 /span nbsp; nbsp; span 1 /span nbsp; nbsp; span 2 /span nbsp; nbsp; span 3 /span /p
p span 2017-01-09 /span nbsp; nbsp; span 4 /span nbsp; nbsp; span 5 /span nbsp; nbsp; span 6 /span nbsp; nbsp; span 7 /span /p
p span 2017-01-10 /span nbsp; nbsp; span 8 /span nbsp; nbsp; span 9 /span nbsp; span 10 /span nbsp; span 11 /span /p
p span 2017-01-11 /span nbsp; span 12 /span nbsp; span 13 /span nbsp; span 14 /span nbsp; span 15 /span /p
p span 2017-01-12 /span nbsp; span 16 /span nbsp; span 17 /span nbsp; span 18 /span nbsp; span 19 /span /p
p span 2017-01-13 /span nbsp; span 20 /span nbsp; span 21 /span nbsp; span 22 /span nbsp; span 23 /span /p
p 挑选第 span 3 /span 行第 span 2 /span 位的数据: /p
p span 2017-01-11 /span nbsp; nbsp; span 13 /span /p
p span 2017-01-12 /span nbsp; nbsp; span 17 /span /p
p span 2017-01-13 /span nbsp; nbsp; span 21 /span /p
p Freq: D, Name: B, dtype: int32 /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 当然,咱们也能够在iloc中运用切片,比方,我想挑选出从第3行之后的第2列数据: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" span import /span pandas span as /span pd /p
p span import /span numpy span as /span np /p
p dates span = /span pd.date_range( span amp;quot;2017-01-08 amp;quot; /span , periods span = /span span 6 /span ) /p
p data span = /span pd.DataFrame(np.arange( span 24 /span ).reshape( span 6 /span , span 4 /span ), index span = /span dates, columns span = /span [ span amp;quot;A amp;quot; /span , span amp;quot;B amp;quot; /span , span amp;quot;C amp;quot; /span , span amp;quot;D amp;quot; /span ]) /p
p span print /span ( span amp;quot;data: amp;quot; /span ) /p
p span print /span (data) /p
p nbsp; /p
p span print /span ( span amp;quot;挑选第3行之后第2列的数据: amp;quot; /span ) /p
p span print /span (data.iloc[ span 3 /span :, span 1 /span ]) /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 输出为: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" data: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;A nbsp; nbsp;B nbsp; nbsp;C nbsp; nbsp;D /p
p span 2017-01-08 /span nbsp; nbsp; span 0 /span nbsp; nbsp; span 1 /span nbsp; nbsp; span 2 /span nbsp; nbsp; span 3 /span /p
p span 2017-01-09 /span nbsp; nbsp; span 4 /span nbsp; nbsp; span 5 /span nbsp; nbsp; span 6 /span nbsp; nbsp; span 7 /span /p
p span 2017-01-10 /span nbsp; nbsp; span 8 /span nbsp; nbsp; span 9 /span nbsp; span 10 /span nbsp; span 11 /span /p
p span 2017-01-11 /span nbsp; span 12 /span nbsp; span 13 /span nbsp; span 14 /span nbsp; span 15 /span /p
p span 2017-01-12 /span nbsp; span 16 /span nbsp; span 17 /span nbsp; span 18 /span nbsp; span 19 /span /p
p span 2017-01-13 /span nbsp; span 20 /span nbsp; span 21 /span nbsp; span 22 /span nbsp; span 23 /span /p
p 挑选第 span 3 /span 行之后第 span 2 /span 列的数据: /p
p span 2017-01-11 /span nbsp; nbsp; span 13 /span /p
p span 2017-01-12 /span nbsp; nbsp; span 17 /span /p
p span 2017-01-13 /span nbsp; nbsp; span 21 /span /p
p Freq: D, Name: B, dtype: int32 /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 咱们也能够单独地挑选某几行的数据,例如: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" span import /span pandas span as /span pd /p
p span import /span numpy span as /span np /p
p dates span = /span pd.date_range( span amp;quot;2017-01-08 amp;quot; /span , periods span = /span span 6 /span ) /p
p data span = /span pd.DataFrame(np.arange( span 24 /span ).reshape( span 6 /span , span 4 /span ), index span = /span dates, columns span = /span [ span amp;quot;A amp;quot; /span , span amp;quot;B amp;quot; /span , span amp;quot;C amp;quot; /span , span amp;quot;D amp;quot; /span ]) /p
p span print /span ( span amp;quot;data: amp;quot; /span ) /p
p span print /span (data) /p
p nbsp; /p
p span print /span ( span amp;quot;挑选第1,3,5行第1到第3列的数据: amp;quot; /span ) /p
p span print /span (data.iloc[[ span 1 /span , span 3 /span , span 5 /span ], span 1 /span : span 3 /span ]) /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" data: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;A nbsp; nbsp;B nbsp; nbsp;C nbsp; nbsp;D /p
p span 2017-01-08 /span nbsp; nbsp; span 0 /span nbsp; nbsp; span 1 /span nbsp; nbsp; span 2 /span nbsp; nbsp; span 3 /span /p
p span 2017-01-09 /span nbsp; nbsp; span 4 /span nbsp; nbsp; span 5 /span nbsp; nbsp; span 6 /span nbsp; nbsp; span 7 /span /p
p span 2017-01-10 /span nbsp; nbsp; span 8 /span nbsp; nbsp; span 9 /span nbsp; span 10 /span nbsp; span 11 /span /p
p span 2017-01-11 /span nbsp; span 12 /span nbsp; span 13 /span nbsp; span 14 /span nbsp; span 15 /span /p
p span 2017-01-12 /span nbsp; span 16 /span nbsp; span 17 /span nbsp; span 18 /span nbsp; span 19 /span /p
p span 2017-01-13 /span nbsp; span 20 /span nbsp; span 21 /span nbsp; span 22 /span nbsp; span 23 /span /p
p 挑选第 span 3 /span 行之后第 span 2 /span 列的数据: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;B nbsp; nbsp;C /p
p span 2017-01-09 /span nbsp; nbsp; span 5 /span nbsp; nbsp; span 6 /span /p
p span 2017-01-11 /span nbsp; span 13 /span nbsp; span 14 /span /p
p span 2017-01-13 /span nbsp; span 21 /span nbsp; span 22 /span /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; h1 id="标签和方位混合挑选" 标签和方位混合挑选 /h1 nbsp; /p
p nbsp; p 比方行用数字来挑选,而列用标签来进行挑选,例如: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" span import /span pandas span as /span pd /p
p span import /span numpy span as /span np /p
p dates span = /span pd.date_range( span amp;quot;2017-01-08 amp;quot; /span , periods span = /span span 6 /span ) /p
p data span = /span pd.DataFrame(np.arange( span 24 /span ).reshape( span 6 /span , span 4 /span ), index span = /span dates, columns span = /span [ span amp;quot;A amp;quot; /span , span amp;quot;B amp;quot; /span , span amp;quot;C amp;quot; /span , span amp;quot;D amp;quot; /span ]) /p
p span print /span ( span amp;quot;data: amp;quot; /span ) /p
p span print /span (data) /p
p nbsp; /p
p span print /span ( span amp;quot;挑选第1,3,5行第1到第3列的数据: amp;quot; /span ) /p
p span print /span (data.ix[[ span 1 /span , span 3 /span , span 5 /span ], [ span amp;quot;A amp;quot; /span , span amp;quot;C amp;quot; /span ]]) /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 输出为: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" data: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;A nbsp; nbsp;B nbsp; nbsp;C nbsp; nbsp;D /p
p span 2017-01-08 /span nbsp; nbsp; span 0 /span nbsp; nbsp; span 1 /span nbsp; nbsp; span 2 /span nbsp; nbsp; span 3 /span /p
p span 2017-01-09 /span nbsp; nbsp; span 4 /span nbsp; nbsp; span 5 /span nbsp; nbsp; span 6 /span nbsp; nbsp; span 7 /span /p
p span 2017-01-10 /span nbsp; nbsp; span 8 /span nbsp; nbsp; span 9 /span nbsp; span 10 /span nbsp; span 11 /span /p
p span 2017-01-11 /span nbsp; span 12 /span nbsp; span 13 /span nbsp; span 14 /span nbsp; span 15 /span /p
p span 2017-01-12 /span nbsp; span 16 /span nbsp; span 17 /span nbsp; span 18 /span nbsp; span 19 /span /p
p span 2017-01-13 /span nbsp; span 20 /span nbsp; span 21 /span nbsp; span 22 /span nbsp; span 23 /span /p
p 挑选第 span 1 /span , span 3 /span , span 5 /span 行第 span 1 /span 到第 span 3 /span 列的数据: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;A nbsp; nbsp;C /p
p span 2017-01-09 /span nbsp; nbsp; span 4 /span nbsp; nbsp; span 6 /span /p
p span 2017-01-11 /span nbsp; span 12 /span nbsp; span 14 /span /p
p span 2017-01-13 /span nbsp; span 20 /span nbsp; span 22 /span /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; h1 id="依据某列中的数值进行挑选" 依据某列中的数值进行挑选 /h1 nbsp; /p
p nbsp; p 类似于SQL中where column amp;lt; xxx这种类型的挑选。 br / 例如,挑选出A列小于8的数据: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" span import /span pandas span as /span pd /p
p span import /span numpy span as /span np /p
p dates span = /span pd.date_range( span amp;quot;2017-01-08 amp;quot; /span , periods span = /span span 6 /span ) /p
p data span = /span pd.DataFrame(np.arange( span 24 /span ).reshape( span 6 /span , span 4 /span ), index span = /span dates, columns span = /span [ span amp;quot;A amp;quot; /span , span amp;quot;B amp;quot; /span , span amp;quot;C amp;quot; /span , span amp;quot;D amp;quot; /span ]) /p
p span print /span ( span amp;quot;data: amp;quot; /span ) /p
p span print /span (data) /p
p nbsp; /p
p span print /span ( span amp;quot;依据某列中的数值进行挑选: amp;quot; /span ) /p
p span print /span (data[data.A span amp;lt; /span span 8 /span ]) /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 输出为: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" data: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;A nbsp; nbsp;B nbsp; nbsp;C nbsp; nbsp;D /p
p span 2017-01-08 /span nbsp; nbsp; span 0 /span nbsp; nbsp; span 1 /span nbsp; nbsp; span 2 /span nbsp; nbsp; span 3 /span /p
p span 2017-01-09 /span nbsp; nbsp; span 4 /span nbsp; nbsp; span 5 /span nbsp; nbsp; span 6 /span nbsp; nbsp; span 7 /span /p
p span 2017-01-10 /span nbsp; nbsp; span 8 /span nbsp; nbsp; span 9 /span nbsp; span 10 /span nbsp; span 11 /span /p
p span 2017-01-11 /span nbsp; span 12 /span nbsp; span 13 /span nbsp; span 14 /span nbsp; span 15 /span /p
p span 2017-01-12 /span nbsp; span 16 /span nbsp; span 17 /span nbsp; span 18 /span nbsp; span 19 /span /p
p span 2017-01-13 /span nbsp; span 20 /span nbsp; span 21 /span nbsp; span 22 /span nbsp; span 23 /span /p
p 挑选依据某列中的数值进行挑选: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; A nbsp; B nbsp; C nbsp; D /p
p span 2017-01-08 /span nbsp; span 0 /span nbsp; span 1 /span nbsp; span 2 /span nbsp; span 3 /span /p
p span 2017-01-09 /span nbsp; span 4 /span nbsp; span 5 /span nbsp; span 6 /span nbsp; span 7 /span /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 假如想要进行联合索引,比方where A amp;lt;8 and B amp;lt; 5,则: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" span import /span pandas span as /span pd /p
p span import /span numpy span as /span np /p
p dates span = /span pd.date_range( span amp;quot;2017-01-08 amp;quot; /span , periods span = /span span 6 /span ) /p
p data span = /span pd.DataFrame(np.arange( span 24 /span ).reshape( span 6 /span , span 4 /span ), index span = /span dates, columns span = /span [ span amp;quot;A amp;quot; /span , span amp;quot;B amp;quot; /span , span amp;quot;C amp;quot; /span , span amp;quot;D amp;quot; /span ]) /p
p span print /span ( span amp;quot;data: amp;quot; /span ) /p
p span print /span (data) /p
p nbsp; /p
p span print /span ( span amp;quot;依据某列中的数值进行挑选: amp;quot; /span ) /p
p data span = /span data[data.A span amp;lt; /span span 8 /span ] /p
p span print /span (data[data.B span amp;lt; /span span 5 /span ]) /code /pre /p
p nbsp; /div nbsp; /p
p nbsp; p 输出为: /p nbsp; /p
p nbsp; div /p
p nbsp; pre http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" code http://www.cppentry.com/list.php?fid=77" target="_blank">PYTHON" data: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;A nbsp; nbsp;B nbsp; nbsp;C nbsp; nbsp;D /p
p span 2017-01-08 /span nbsp; nbsp; span 0 /span nbsp; nbsp; span 1 /span nbsp; nbsp; span 2 /span nbsp; nbsp; span 3 /span /p
p span 2017-01-09 /span nbsp; nbsp; span 4 /span nbsp; nbsp; span 5 /span nbsp; nbsp; span 6 /span nbsp; nbsp; span 7 /span /p
p span 2017-01-10 /span nbsp; nbsp; span 8 /span nbsp; nbsp; span 9 /span nbsp; span 10 /span nbsp; span 11 /span /p
p span 2017-01-11 /span nbsp; span 12 /span nbsp; span 13 /span nbsp; span 14 /span nbsp; span 15 /span /p
p span 2017-01-12 /span nbsp; span 16 /span nbsp; span 17 /span nbsp; span 18 /span nbsp; span 19 /span /p
p span 2017-01-13 /span nbsp; span 20 /span nbsp; span 21 /span nbsp; span 22 /span nbsp; span 23 /span /p
p 依据某列中的数值进行挑选: /p
p nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; A nbsp; B nbsp; C nbsp; D /p
p span 2017-01-08 /span nbsp; span 0 /span nbsp; span 1 /span nbsp; span 2 /span nbsp; span 3 /span /code /pre /p
p nbsp; /div nbsp; /p
p /div /p /div
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表牛牛娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章

阅读排行

  • 1

    java多线程(七)ITeye

    线程,倾向,目标
  • 2

    java线程池ITeye

    线程,使命,工人
  • 3
  • 4
  • 5

    修饰符ITeye

    润饰,能够,直接
  • 6
  • 7
  • 8

    第02章 根底中心ITeye

    目标,根底,中心
  • 9
  • 10