> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Pandas vs R - cheat sheet
- URL: https://datascientyst.com/pandas-vs-r-cheat-sheet/
- Published: 2023-12-01T15:49:52.000Z
- Updated: 2023-12-02T22:34:21.000Z
- Author: John D K
- Tags: Cheat Sheet

This is a **Python/Pandas vs R cheatsheet** for a quick reference for switching between both. The post contains **equivalent operations between Pandas and R**. The post includes the most used operations needed on a daily baisis for data analysis.

Have in mind that some examples might differ due to different indexing or updates.

If you want to contribute feel free to suggest changes or additions on GitHub: [pandas\_r\_cheatsheet.csv](https://github.com/softhints/Pandas-Tutorials/blob/master/cheatsheet/pandas%5Fr%5Fcheatsheet.csv?ref=datascientyst.com)

## Pandas vs R cheatsheet

Single column 2 columns 3 columns Hide navigation Hide TOC 

## Setup

Import and package installation

![](https://datascientyst.com/content/images/2022/04/pandas_cheat_sheet_basics.png)

`import pandas as pd import numpy as np`

library(dplyr) library(ggplot2) # error if missing require(ggplot2) # warning

Import libraries and modules

`pip install pandas`

install.packages('ggplot2')

install package

`https://pypi.org/`

https://cran.r-project.org/web/packages/

Search Packages

## Data Structures

Pandas Series vs R Array DataFrame comparison

![](https://datascientyst.com/content/images/2022/04/pandas_cheat_sheet_data_structures-1.png)

`s = pd.Series(np.arange(5))`

s <- 0:4 # >

Pandas series vs R vectors

`s[0]`

s\[0\]

Get first element of array or Series

`df = pd.DataFrame( {'col_1': [11, 12, 13], 'col_2': [21, 22, 23]}, index=[0, 1, 3])`

df = data.frame ( col\_1 = c(11, 12, 13), col\_2 = c(21, 22, 23) ) rownames(df) <- c(0,1,3) # >

Pandas vs R DataFrame

`import numpy as np import pandas as pd data = np.random.randn(10, 3) cols = list('abc') pd.DataFrame(data, columns=cols)`

data.frame(a=rnorm(10), b=rnorm(10), c=rnorm(10))

Create random DataFrame

## Read

Import Data R vs Pandas

![](https://datascientyst.com/content/images/2022/04/pandas_cheat_sheet_read.png)

`df = pd.read_csv('file.csv')`

df <- read.csv('file.csv') # >

Read CSV file

`pd.read_json('file.json')`

library(jsonlite) df <- read\_json('file.json') # >

Read JSON file

`pd.read_csv('https://example.com/file.csv')`

read.csv(url('https://example.com/file.csv'))

Read data from URL

`df = pd.read_fwf('delim_file.txt')`

df <- read\_fwf('delim\_file.txt') # >

Read delimited file

## Write

Data export - Pandas vs R

![](https://datascientyst.com/content/images/2022/04/pandas_cheat_sheet_write.png)

`df.to_csv('file.csv')`

write.csv(df, 'data.csv', row.names=FALSE)

Writes to a CSV file

`df.to_json('file.json')`

js\_file <- jsonlite::toJSON(df2, pretty = TRUE) write(js\_file, 'file.json') # >

Writes to a file in JSON format

## Inspect Data

Statistics, samples and summary of the data

![](https://datascientyst.com/content/images/2022/04/pandas_cheat_sheet_info.png)

`df.shape`

dim(df)

return dimensions

`df.head(6)`

head(df, 6)

First n rows

`df.tail(6)`

tail(df, 6)

Last n rows

`df.describe()`

summary(df)

Summary statistics

`df.loc[:, :'a'].describe()`

summary(df\[, 'a'\])

Describe columns

`df['A'].mean()`

mean(df\[, 'a'\])

Statistical functions

` df.sample(n=10)`

sample\_n(df, 10)

Sample n random rows

## Select

Select data by index, by label, get subset

![](https://datascientyst.com/content/images/2022/04/pandas_cheat_sheet_select.png)

`df.loc[1:3, :]`

df\[2:4,\]

Select first N rows - all columns

`df.loc[[1, 2, 3], :]`

df\[c(2,3,4),\]

Select rows by index

`df.loc[:, ['a', 'b']].copy()`

copy <-data.frame(df\[,c('a','b')\]) # >

Select columns by name(copy)

`df.loc[:, ['a']]`

df\[, 'a'\]

Select columns by name(reference)

`df.loc[1:3, ['b', 'a']]`

df\[2:4, c('b','a')\]

Subset rows and columns

`df.loc[[3,1], ['b', 'a']]`

df\[4:2, c('b','a')\]

Reverse selection

`df[df['a'].isna()]`

df\[is.na(df$a), \]

Select NaN values

`df['a'].dropna()`

df\[!is.na(df$a), \]

Select non NaN values

## Add rows/columns

Add new columns and rows

![](https://datascientyst.com/content/images/2022/04/pandas_cheat_sheet_add.png)

`df['new col'] = df['col'] * 100`

df$new <- df\[, 'a'\] \* 100 # >

Add new column based on other column

`df['new col'] = False`

df$new <-FALSE # >

Add new column single value

`df.loc[-1] = [1, 2, 3]`

df\[nrow(df) + 1,\] = c(1,2,3)

Add new row at the end of DataFrame

`df.append(df2, ignore_index = True)`

rbind(df, df2)

add rows from DataFrame to existing DataFrame

## Drop rows/columns/nan

Drop data from DataFrame

![](https://datascientyst.com/content/images/2022/04/pandas_cheat_sheet_drop.png)

`s.drop(1)`

s\[!(s == 1)\]

(Series) Drop values from Series by index (row axis)

`s.drop([1, 2])`

s\[!(s %in% c(1,2))\]

(Series) Drop values from Series by index (row axis)

`df.drop('b' , axis=1) `

subset(df, select = -c(b))

Drop column by name col\_1 (column axis)

`df.dropna()`

library(tidyr) df %>% drop\_na()

Drops all rows that contain null values

`df.dropna(axis=1)`

janitor::remove\_empty(df, which = 'cols')

Drops all columns that contain null values

## Sort values/index

Sorting and rank values in Pandas vs R

![](https://datascientyst.com/content/images/2022/04/pandas_cheat_sheet_sort.png)

`sorted([2,3,1])`

sort(c(2,3,1))

sort array of values

`sorted([2,3,1], reverse=True)`

sort(c(2,3,1),decreasing=TRUE)

sort in reverse order

`df['a'].sort_values()`

sort(df\[, 'a'\])

sort DataFrame by column

`df.sort_values(['a', 'b'], ascending=[False, True])`

df\[order(-df$a, df$b), \]

sort DataFrame by multiple columns

## Filter

Filter data based on multiple criteria

![](https://datascientyst.com/content/images/2022/04/pandas_cheat_sheet_filter.png)

`df.loc[:, df.isna().any()]`

apply(df, 2, function(x) any(is.na(x)))

find columns with na

`df.loc[df.isna().any(), :]`

apply(df, 1, function(x) any(is.na(x)))

find rows with na

`df[df['col_1'] > 100]`

filter(df, col\_1 > 100)

Values greater than X

`df[(df['a']=='a')&(df['b']>=10)]`

filter(df, a == 'a', b > 10)

Filter Multiple Conditions - & - and; | - or

`df[df['a'] == 'test']`

filter(df, a == 'test')

filter by sting value

`df[(df['a'] == 'test') & (df['b'] == 'a2') ]`

filter(df, a == 'test', b == 'a2' )

combine conditions

## Group by

Group by and summarize data

![](https://datascientyst.com/content/images/2022/04/pandas_cheat_sheet_groupby.png)

`df.groupby('a')`

group\_by(df, 'a')

Group by single column

`df.groupby(['a', 'b']).c.sum()`

aggregate(df$b, by=list(a=df$a), FUN=sum)

group by multiple columns and sum third

`df['a'].value_counts()`

dplyr::count(df, a, sort = TRUE)

group by and count

## Convert

Convert to date, string, numeric

![](https://datascientyst.com/content/images/2022/04/pandas_cheat_sheet_convert.png)

`df['a'].fillna(0)`

library(dplyr) df <- df %>% mutate(a = if\_else(is.na(a), 0, a)) # >

replace NA values

`df.replace('..', None)`

df\[df == '..'\] <- NA # >

convert .. to NA

`df['col_1'].astype('int64')`

strtoi(c('1', '2'), base = 0L)

convert string to int

`pd.to_datetime(df['date'], format='%Y-%m-%d')`

dates <- c('2023-09-04', '2023-09-06') as.Date(dates, format='%Y-%m-%d') # >

convert string to date

P.S. Due to bug in the blog platform `<-` is displayed with R comment. So instead of: `s <- 0:4` the code is shown as `s <- 0:4 #>`

## 0\. How to Install R Packages

To install new packages in R follow these steps:

- Launch your R console or RStudio.
- Install single package  
  - `install.packages('jsonlite')`
- To install multiple packages simultaneously:  
  - `install.packages(c('jsonlite', 'ggplot2'))`
- R will download and install the specified packages from the CRAN (Comprehensive R Archive Network) repository.

Once the installation is complete, you can load the package into your R session using the `library('jsonlite')` function.

### Install ggplot2 in R

For example, to install the "ggplot2" package, you can use the commands:

```python
install.packages('jsonlite')
library('jsonlite')

```

## 1\. Main Differences: R and Pandas

Pandas and R are both popular tools/languages for data analysis, manipulation and statistics. Some key differences between them:

### Indexing

One big difference between R and Pandas is indexing:

- R - 1 based  
\* [Indexing from zero in R](https://www.r-bloggers.com/2021/12/indexing-from-zero-in-r/?ref=datascientyst.com)  
\* [Package ‘index0’](https://cran.r-project.org/web/packages/index0/index0.pdf?ref=datascientyst.com)  
\* Pandas - 0 based

### Syntax

- R syntax is tailored for statistical analysis. It uses functions and operators that are well-suited for data manipulation, statistics and visualization.
- Pandas uses Python syntax, which is more general-purpose. It leverages Python's data structures like DataFrames and Series for data manipulation. Pandas also use the indexing, slicing and other Python techniques.

Below you can compare the creation of DataFrames in Pandas vs R:

```python
# pandas
import pandas as pd
df = pd.DataFrame(np.random.randn(10, 5), columns=list("abcd"))

df[["a", "c", "d"]]

```

vs

```python
# R
df <- data.frame(a=rnorm(10), b=rnorm(10), c=rnorm(10), d=rnorm(10))
df[, c("a", "c", "d")]

```

### Data Structures

- R - uses data structures like:  
  - Vectors
  - Lists
  - Matrices
  - Dataframes
- Pandas - [Intro to data structures](https://pandas.pydata.org/pandas-docs/stable/user%5Fguide/dsintro.html?ref=datascientyst.com)  
  - DataFrames
  - Series

DataFrames are the primary data structure for data analysis in R and Pandas.

### Performance

R is considered to be faster for most operations in comparison to Pandas. For smaller datasets Pandas might be close to R.

To test performance we can use dataset with 2GB/10M rows - [Game Recommendations on Steam](https://www.kaggle.com/datasets/antonkozyriev/game-recommendations-on-steam?select=recommendations.csv&ref=datascientyst.com):

```python
# pandas
%%time
import pandas as pd
df = pd.read_csv('recommendations.csv')
df['hours'].mean()

# R
library(microbenchmark)
microbenchmark(df <- read.csv('recommendations.csv'), mean(df[, 'hours']))
end

```

The results are:

- Pandas

```python
CPU times: user 17.1 s, sys: 4.38 s, total: 21.5 s
Wall time: 23.7 s
103.97299330788391

```

- R timing

| expr                  | min  | lq   | mean | median | uq   | max neval |    |
| --------------------- | ---- | ---- | ---- | ------ | ---- | --------- | -- |
| df <- read.csv        | 141  | 141  | 142  | 141    | 142  | 143       | 10 |
| mean(df\[, "hours"\]) | 0.11 | 0.11 | 0.11 | 0.11   | 0.11 | 0.11      | 10 |

As we can see times are close for R and Pandas for this use case.

### Package Ecosystem

Both offer mature package systems with a wide variety of packages related to data analysis and visualization.

- R has a vast repository of packages on CRAN (Comprehensive R Archive Network) dedicated to statistics, data analysis, and visualization.
- Pandas is part of the Python ecosystem, which has a broader range of packages for various purposes beyond data analysis.

### Community

- R has a strong community of experienced statisticians and data analysts, and there are numerous resources and documentation available for R users.
- Pandas benefits from the larger Python community, which offers extensive resources and documentation for data analysis and programming in general. People from different scientific areas join Python and Pandas communities to solve everyday problems.

### Learning Curve

Again it depends on personal choice. Python is considered as one of the best programming languages for beginners. R is far below Python in recent surveys for loved language:

[stackoverflow survey - Most loved, dreaded, and wanted](https://survey.stackoverflow.co/2022/?ref=datascientyst.com#technology-most-loved-dreaded-and-wanted)

## 3\. Pandas vs R - useful links

|                 | Pandas                                                                                                                                          | R                                                                                                                                                  |
| --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
|                 | data analysis tool                                                                                                                              | language for statistical computing                                                                                                                 |
| site            | [https://pandas.pydata.org/](https://pandas.pydata.org/?ref=datascientyst.com)                                                                  | [https://www.r-project.org/](https://www.r-project.org/?ref=datascientyst.com)                                                                     |
| docs            | [https://pandas.pydata.org/docs/](https://pandas.pydata.org/docs/?ref=datascientyst.com)                                                        | [https://cran.r-project.org/manuals.html](https://cran.r-project.org/manuals.html?ref=datascientyst.com)                                           |
| packages        | [https://pypi.org/](https://pypi.org/?ref=datascientyst.com)                                                                                    | [https://cran.r-project.org/web/packages/](https://cran.r-project.org/web/packages/?ref=datascientyst.com)                                         |
| repo            | [https://github.com/pandas-dev/pandas](https://github.com/pandas-dev/pandas?ref=datascientyst.com)                                              | \-                                                                                                                                                 |
| cheatsheet      | [Data Wrangling with pandas](https://pandas.pydata.org/Pandas%5FCheat%5FSheet.pdf?ref=datascientyst.com)                                        | [Data Wrangling with dplyr and tidyr](https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf?ref=datascientyst.com)      |
| basics          | [https://pandas.pydata.org/docs/user\_guide/basics.html](https://pandas.pydata.org/docs/user%5Fguide/basics.html?ref=datascientyst.com)         | [https://cran.r-project.org/doc/manuals/r-release/R-intro.pdf](https://cran.r-project.org/doc/manuals/r-release/R-intro.pdf?ref=datascientyst.com) |
| getting started | [https://pandas.pydata.org/docs/getting\_started/index.html](https://pandas.pydata.org/docs/getting%5Fstarted/index.html?ref=datascientyst.com) | [https://education.rstudio.com/learn/beginner/](https://education.rstudio.com/learn/beginner/?ref=datascientyst.com)                               |
| indexing        | 0 based                                                                                                                                         | 1 based                                                                                                                                            |
| missing value   | np.nan                                                                                                                                          | NA                                                                                                                                                 |
| Boolean         | False/True                                                                                                                                      | FALSE/TRUE                                                                                                                                         |
| Comments        | \# comment                                                                                                                                      | \# comment                                                                                                                                         |

## 4\. Summary & Resources

In summary, Pandas and R are both powerful tools for data analysis, visualization and manipulation.

Ultimately, the choice between R and Pandas often depends on your specific needs, existing familiarity with a programming language, and the ecosystem of packages that best suit your data analysis tasks.

Personally I find Pandas easier to learn and start because of the previous experience in Python language. Knowing Pandas or R makes it easier to transition to the other one.

- [Comparison with R / R libraries](https://pandas.pydata.org/docs/getting%5Fstarted/comparison/comparison%5Fwith%5Fr.html?ref=datascientyst.com)
- [Pandas Cheat Sheet for Data Science](https://datascientyst.com/pandas-cheat-sheet-for-data-science/)
- [Pandas vs SQL Cheat Sheet](https://datascientyst.com/pandas-vs-sql-cheat-sheet/)
- [Pandas vs Julia - cheat sheet and comparison](https://datascientyst.com/pandas-vs-julia-comparison-cheat-sheet/)
- [pandas notebook](https://github.com/softhints/Pandas-Exercises-Projects/blob/main/cheat%5Fsheet/pandas%5Fjulia/pandas.ipynb?ref=datascientyst.com)

## 5\. Pandas vs R Cheat Sheet Image

Dark version:

![](https://datascientyst.com/content/images/2023/12/Pandas-vs-R-dark.webp)

Light Version:

![Pandas vs R light.webp](https://datascientyst.com/content/images/2023/12/Pandas-vs-R-light.webp)

## 6\. Pandas vs R comparison

We are working on a visual comparison between R and Pandas. Below you can find a quick teaser:

![pandas vs R comparison.webp](https://datascientyst.com/content/images/2023/12/pandas-vs-R-comparison.webp)

P.S. We were overloaded in the last year so we were not able to post frequently. We hope to have more time for this project and data science.