Skip to content

Commit f94a8d5

Browse files
Merge pull request pandas-dev#6499 from cpcloud/r-slicing-with-c
DOC: show users how to emulate R c function with iloc slicing and r_
2 parents 8cdd721 + 698787a commit f94a8d5

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

doc/source/comparison_with_r.rst

+40
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,43 @@ R packages.
3030
Base R
3131
------
3232

33+
Slicing with R's |c|_
34+
~~~~~~~~~~~~~~~~~~~~~
35+
36+
R makes it easy to access ``data.frame`` columns by name
37+
38+
.. code-block:: r
39+
40+
df <- data.frame(a=rnorm(5), b=rnorm(5), c=rnorm(5), d=rnorm(5), e=rnorm(5))
41+
df[, c("a", "c", "e")]
42+
43+
or by integer location
44+
45+
.. code-block:: r
46+
47+
df <- data.frame(matrix(rnorm(1000), ncol=100))
48+
df[, c(1:10, 25:30, 40, 50:100)]
49+
50+
Selecting multiple columns by name in ``pandas`` is straightforward
51+
52+
.. ipython:: python
53+
54+
df = DataFrame(np.random.randn(10, 3), columns=list('abc'))
55+
df[['a', 'c']]
56+
df.loc[:, ['a', 'c']]
57+
58+
Selecting multiple noncontiguous columns by integer location can be achieved
59+
with a combination of the ``iloc`` indexer attribute and ``numpy.r_``.
60+
61+
.. ipython:: python
62+
63+
named = list('abcdefg')
64+
n = 30
65+
columns = named + np.arange(len(named), n).tolist()
66+
df = DataFrame(np.random.randn(n, n), columns=columns)
67+
68+
df.iloc[:, np.r_[:10, 24:30]]
69+
3370
|aggregate|_
3471
~~~~~~~~~~~~
3572

@@ -407,6 +444,9 @@ The second approach is to use the :meth:`~pandas.DataFrame.groupby` method:
407444
For more details and examples see :ref:`the reshaping documentation
408445
<reshaping.pivot>` or :ref:`the groupby documentation<groupby.split>`.
409446

447+
.. |c| replace:: ``c``
448+
.. _c: http://stat.ethz.ch/R-manual/R-patched/library/base/html/c.html
449+
410450
.. |aggregate| replace:: ``aggregate``
411451
.. _aggregate: http://finzi.psych.upenn.edu/R/library/stats/html/aggregate.html
412452

0 commit comments

Comments
 (0)