# Translating R Functions in its Tidyverse Set of Packages to Python Pandas Code

If you’re a data scientist working in both R and Python, it’s inevitable that you’ll need to translate functions from one to the other. The Tidyverse set of packages in R provides a collection of powerful tools for performing data manipulation and analysis. In this article, we’ll explore how to translate some of the most commonly used Tidyverse functions into the equivalent Python Pandas code.

## dplyr

The dplyr package is one of the most widely used components of the Tidyverse. It provides a grammar for data manipulation that makes it easy to filter, arrange, and summarize data. Here are some of the most commonly used dplyr functions and their Pandas equivalents:

### filter()

The filter() function allows you to select a subset of rows from a data frame based on a specified condition. Here’s the R code:

```
library(dplyr)
data_frame(x = c(1,2,3), y = c("a", "b", "c")) %>%
filter(x > 1)
```

In Pandas, this would look like:

```
import pandas as pd
df = pd.DataFrame({'x': [1, 2, 3], 'y': ['a', 'b', 'c']})
df[df['x'] > 1]
```

### arrange()

The arrange() function is used to reorder the rows in a data frame. Here’s the R code:

```
library(dplyr)
data_frame(x = c(1,2,3), y = c("a", "b", "c")) %>%
arrange(desc(x))
```

In Pandas, we can use the sort_values() function:

```
import pandas as pd
df = pd.DataFrame({'x': [1, 2, 3], 'y': ['a', 'b', 'c']})
df.sort_values(by='x', ascending=False)
```

### select()

The select() function is used to select specific columns from a data frame. Here’s the R code:

```
library(dplyr)
data_frame(x = c(1,2,3), y = c("a", "b", "c"), z = c("d", "e", "f")) %>%
select(x, y)
```

In Pandas, we can use the loc[] indexer:

```
import pandas as pd
df = pd.DataFrame({'x': [1, 2, 3], 'y': ['a', 'b', 'c'], 'z': ['d', 'e', 'f']})
df.loc[:, ['x', 'y']]
```

## ggplot2

The ggplot2 package is used for creating visualizations in R. It provides a powerful system for creating plots with a consistent syntax. Here are some of the most commonly used ggplot2 functions and their Python equivalents:

### ggplot()

The ggplot() function is used to initialize a plot. Here’s the R code:

```
library(ggplot2)
ggplot(mpg, aes(x=displ, y=hwy)) +
geom_point()
```

In Python, we can use the seaborn library:

```
import seaborn as sns
mpg = sns.load_dataset('mpg')
sns.scatterplot(x='displ', y='hwy', data=mpg)
```

### geom_point()

The geom_point() function is used to add points to a plot. Here’s the R code:

```
library(ggplot2)
ggplot(mpg, aes(x=displ, y=hwy)) +
geom_point()
```

In Python, we can use the seaborn library:

```
import seaborn as sns
mpg = sns.load_dataset('mpg')
sns.scatterplot(x='displ', y='hwy', data=mpg)
```

### geom_line()

The geom_line() function is used to add lines to a plot. Here’s the R code:

```
library(ggplot2)
ggplot(economics_long, aes(x=date, y=value, color=variable)) +
geom_line()
```

In Python, we can use the matplotlib library:

```
import pandas as pd
import matplotlib.pyplot as plt
economics_long = pd.read_csv('https://raw.githubusercontent.com/guru99-edu/R-Programming/master/economics.csv')
economics_long['date'] = pd.to_datetime(economics_long['date'])
plt.plot(economics_long['date'], economics_long['value'])
plt.show()
```

## Conclusion

In this article, we’ve explored how to translate some of the most commonly used Tidyverse functions into the equivalent Python Pandas code. While the syntax may differ somewhat between the two languages, the underlying principles remain the same. By understanding both R and Python, data scientists can become more versatile and able to work with a wider range of datasets and tools.