Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a python/pandas equivalent to R's `with`?

Tags:

python

pandas

r

In R I can have a data.frame or a list with several arguments, and I can operate on them using the with function. For example:

d <- data.frame(x = 1:3, y = 2:4, z = 3:5)
# I can use:
d$x+d$y*d$z-5
# Or, more simply, I can use:
with(d, x+y*z-5)
# [1]  2  9 18

In pandas DataFrame I can use:

d = {'x': [1, 2, 3], 'y': [2, 3, 4], 'z': [3, 4, 5]}
df = pd.DataFrame(data=d)
df.x+df.y*df.z-5
# 0     2
# 1     9
# 2    18
# dtype: int64

But is there a way to do some "with" like statement?

like image 926
Tal Galili Avatar asked Dec 14 '20 07:12

Tal Galili


People also ask

What is the pandas equivalent in R?

Pandas for Python and Dplyr for R are the two most popular libraries for working with tabular/structured data for many Data Scientists.

Which Python function is like R?

The Apply Function in Python The pandas package for Python also has a function called apply, which is equivalent to its R counterpart; the following code illustrates how to use it.

Is pandas an R package?

The PANDA R package (Preferential Attachment based common Neighbor Distribution derived Associations) was designed to perform the following tasks: (1) identify significantly functionally associated protein pairs, (2) predict GO and KEGG terms for proteins, (3) make a cluster of proteins based on the significant protein ...

Is there a dplyr equivalent in Python?

Dplython. Package dplython is dplyr for Python users. It provide infinite functionality for data preprocessing.


1 Answers

One idea is use DataFrame.eval if need processing some columns names some simple arithmetic operations:

print (df.x+df.y*df.z-5)
0     2
1     9
2    18
dtype: int64

print (df.eval('x+y*z-5'))
0     2
1     9
2    18
dtype: int64
like image 124
jezrael Avatar answered Oct 11 '22 16:10

jezrael