Extract Rows/Columns from A Dataframe in Python & R | by Yufeng | Towards Data Science

I’ve been working with data for long. However, I sometimes still need to google “How to extract rows/columns from a data frame in Python/R?” when I change from one language environment to the other.
I am pretty sure that I have done the same for thousands of times, but it seems that my brain refuses to store the commands in memory.
You must know my feeling if you need to work with R and Python simultaneously for data manipulation.
Therefore, I would like to summarize in this article the usage of R and Python in extracting rows/columns from a data frame and make a simple cheat sheet image for the people who need it.
To note, I will only use Pandas in Python and basic functions in R for the purpose of comparing the command lines side by side. Some comprehensive library, ‘dplyr’ for example, is not considered. And I am trying my best to keep the article short.
Let’s begin.
We will use a toy dataset of Allen Iverson’s game stats in the entire article. The dimension and head of the data frame are shown below.
# Rdim(df)head(df)
# Pythondf.shapedf.head()
First, let’s extract the rows from the data frame in both R and Python. In R, it is done by simple indexing, but in Python, it is done by .iloc. Let’s check the examples below.
# R## Extract the third rowdf[3,]## Extract the first three rowsdf[1:3,]### or ###df[c(1,2,3),]
which yields,
# Python## Extract the third rowdf.iloc[2]### or ###df.iloc[2,]### or ###df.iloc[2,:]## Extract the first three rowsdf.iloc[:3]### or ###df.iloc[0:3]### or ###df.iloc[0:3,:]
which yields,
Please note that in the example of extracting a single row from the data frame, the output in R is still in the data frame format, but the output in Python is in the Pandas Series format. This is an essential difference between R and Python in extracting a single row from a data frame.
Similarly, we can extract columns from the data frame.
# R## Extract the 5th columndf[,5]## Extract the first 5 columnsdf[,1:5]
which yields,
# Python## Extract the 5th columndf.iloc[:,4]## Extract the first 5 columnsdf.iloc[:,:5]### or ###df.iloc[:,0:5]
which yields,
When extracting the column, we have to put both the colon and comma in the row position within the square bracket, which is a big difference from extracting rows.
In our dataset, the row and column index of the data frame is the NBA season and Iverson’s stats, respectively. We can use those to extract specific rows/columns from the data frame.
For example, we are interested in the season 1999–2000.
# R## Extract 1999-2000 season.df["1999-00",]## Extract 1999-2000 and 2001-2002 seasons.df[c("1999-00","2000-01"),]
which yields,
# Python## Extract 1999-2000 season.df.loc["1999-00"]## Extract 1999-2000 and 2001-2002 seasons.df.loc[["1999-00","2000-01"]]
which yields,
Please note again that in Python, the output is in Pandas Series format if we extract only one row/column, but it will be Pandas DataFrame format if we extract multiple rows/columns.
When we are only interested in a subset of columns, we can also add the column index.
# R## Extract Iverson's team and minutes played in the 1999-2000 season.df["1999-00",c("Tm","MP")]
which yields,
# Python## Extract Iverson's team and minutes played in the 1999-2000 season.df.loc["1999-00",["Tm","MP"]]
which yields,
In addition to extracting rows/columns by index, we can also do the subsetting based on conditions. For example, we want to extract the seasons in which Iverson has played more than 3000 minutes.
# R## Extract MP over 3kdf[df$MP > 3000,] ## the comma cannot be omitted
which yields,
# Python## Extract MP over 3kdf.loc[df.MP > 3000,:] ## both the comma and colon can be omitted
which yields,
Of course, more complicated conditions can be passed to the square bracket, which only needs a True/False list with the length of the row number of the data frame.
For example, we want to extract the seasons in which Iverson’s true shooting percentage (TS%) is over 50%, minutes played is over 3000, and position (Pos) is either shooting guard (SG) or point guard (PG).
# R## Extract rows with TS.> 50%, MP > 3000 and Pos is SG/PG### define condition as cond_cond_ = (df$TS. > 0.5) & (df$MP > 3000) & (df$Pos %in% c("SG","PG"))df[cond_,]
which yields,
# Python## Extract rows with TS.> 50%, MP> 3000 and Pos is SG/PG### define condition as cond_cond_ = (df["TS."] > 0.5) & (df["MP"] > 3000) & (df["Pos"].isin(["SG","PG"]))df.loc[cond_,:] ## both the comma and colon can be omitted
which yields,
We can apply any kind of boolean values in the “cond_” position.
Here is the cheat sheet that I hope can save your time when you work with both Python and R as I do.
That’s it! A simple summary of table slicing in R/Pandas. I hope it helps!
Thank you for reading! If you like the article, please follow me on Medium. Here are some of my previous articles in data science: