Ben
Ben

Reputation: 4984

Access DataFrame object in DataFrame method call

Instead of using the name of the dataframe object I am calling the method on, is there a shorthand name for it? Eg., Suppose I want to do something like

   long_data_frame_name.long_column_name.fillna(long_data_frame_name.long_column_name.mean())

I'd like to be able to shorten this into something like

  long_data_frame_name.long_column_name.fillna(self.mean())

Self doesn't work (I tried it :)). I think in R plyr there's . which does what I'm asking.

Edit: Just to be clear I know I can assign my long winded variable to another variable but I was hoping to avoid cluttering up the name space with temporary variables.

Upvotes: 1

Views: 2221

Answers (1)

mfitzp
mfitzp

Reputation: 15545

No, but you can create a shorthand reference beforehand. For example:

ldfn = long_data_frame_name.long_column_name

and then do:

ldfn.fillna(ldfn.mean(), inplace=True)

The inplace=True is required, because otherwise .fillna will return a new DataFrame object rather than updating the one you provide.

Here is a complete example. First we set up the demo DataFrame with a missing value:

import numpy as np
import pandas as pd

df = pd.DataFrame(np.ones((5,5)), columns=['aa','bb','cc','dd','ee'])
df.iloc[2,2] = np.nan
df

   aa   bb   cc   dd   ee
0  1.0  1.0  1.0  1.0  1.0
1  1.0  1.0  1.0  1.0  1.0
2  1.0  1.0  NaN  1.0  1.0
3  1.0  1.0  1.0  1.0  1.0
4  1.0  1.0  1.0  1.0  1.0

Next we fill, using inplace=True, and see the fill applied on the original DataFrame object df:

dflc = df.cc
dflc.fillna(dflc.mean(), inplace=True)
df

    aa   bb   cc   dd   ee
0  1.0  1.0  1.0  1.0  1.0
1  1.0  1.0  1.0  1.0  1.0
2  1.0  1.0  1.0  1.0  1.0
3  1.0  1.0  1.0  1.0  1.0
4  1.0  1.0  1.0  1.0  1.0    

Upvotes: 1

Related Questions