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

Pandas vs R cheatsheet


Import and package installation
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 package
Search Packages

Data Structures

Pandas Series vs R Array DataFrame comparison
s = pd.Series(np.arange(5))
s <- 0:4 #>
Pandas series vs R vectors
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


Import Data R vs Pandas
df = pd.read_csv('file.csv')
df <- read.csv('file.csv') #>
Read CSV file
library(jsonlite) df <- read_json('file.json') #>
Read JSON file
Read data from URL
df = pd.read_fwf('delim_file.txt')
df <- read_fwf('delim_file.txt') #>
Read delimited file


Data export - Pandas vs R
write.csv(df, 'data.csv', row.names=FALSE)
Writes to a CSV file
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
return dimensions
head(df, 6)
First n rows
tail(df, 6)
Last n rows
Summary statistics
df.loc[:, :'a'].describe()
summary(df[, 'a'])
Describe columns
mean(df[, 'a'])
Statistical functions
sample_n(df, 10)
Sample n random rows


Select data by index, by label, get subset
df.loc[1:3, :]
Select first N rows - all columns
df.loc[[1, 2, 3], :]
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[$a), ]
Select NaN values
df[!$a), ]
Select non NaN values

Add rows/columns

Add new columns and rows
df['new col'] = df['col'] * 100
df$new <- 100 df[, 'a'] * #>
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
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)
library(tidyr) df %>% drop_na()
Drops all rows that contain null values
janitor::remove_empty(df, which = 'cols')
Drops all columns that contain null values

Sort values/index

Sorting and rank values in Pandas vs R
sort array of values
sorted([2,3,1], reverse=True)
sort in reverse order
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 data based on multiple criteria
df.loc[:, df.isna().any()]
apply(df, 2, function(x) any(
find columns with na
df.loc[df.isna().any(), :]
apply(df, 1, function(x) any(
find rows with na
df[df['col_1'] > 100]
filter(df, col_1 > 100)
Values greater than X
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
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
dplyr::count(df, a, sort = TRUE)
group by and count


Convert to date, string, numeric
library(dplyr) df <- df %>% mutate(a = if_else(, 0, a)) # >
replace NA values
df.replace('..', None)
df[df == '..'] <- na #>
convert .. to NA
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'), 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:


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:


One big difference between R and Pandas is indexing:


  • 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:

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

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


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

Data Structures

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


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:

# pandas
import pandas as pd
df = pd.read_csv('recommendations.csv')

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

The results are:

  • Pandas
CPU times: user 17.1 s, sys: 4.38 s, total: 21.5 s
Wall time: 23.7 s
  • 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.


  • 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

Pandas R
data analysis tool language for statistical computing
repo -
cheatsheet Data Wrangling with pandas Data Wrangling with dplyr and tidyr
getting started
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.

5. Pandas vs R Cheat Sheet Image

Dark version:

Light Version:

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

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.