SDani
SDani

Reputation: 81

Using .loc based on a freshly assingned column in pandas method chaining

I have a pandas dataframe roughly like this:

df = pd.DataFrame([{'Port': 8080, 'Speed': '1940 ms', 'Type': 'HTTP'},
 {'Port': 3128, 'Speed': '1660 ms', 'Type': 'HTTP'},
 {'Port': 3128, 'Speed': '900 ms', 'Type': 'HTTP'},
 {'Port': 8080, 'Speed': '1600 ms', 'Type': 'HTTP'},
 {'Port': 8080, 'Speed': '1340 ms', 'Type': 'HTTP'}])

I'm adding a new column, then use that column to filter the dataframe, then some other modifications later on. I was wondering if I can chain all these function calls together into one statement:

new_df = (
  df
  .assign(sp_int = df. Speed.apply(lambda x: int(x.replace(' ms', ''))))
  .loc[]
  ...
)

I know the filtering step can be pushed in front of the assignment, but that would look terrible:

new_df = (
  df
  .loc[ df.Speed.apply(lambda x: int(x.replace(' ms', '')) < 1000) ]
  .assign(sp_int = df. Speed.apply(lambda x: int(x.replace(' ms', ''))))
  ...
)

It would be amazing to have something neater.

Thanks for the insights!

Upvotes: 1

Views: 628

Answers (2)

Mykola Zotko
Mykola Zotko

Reputation: 17824

You can also use query:

df.assign(sp_int = df.Speed.str.replace(' ms', '').astype(int)).query('sp_int < 1000')

Upvotes: 1

Quang Hoang
Quang Hoang

Reputation: 150745

Yes, you can do:

new_df = (
  df
  .assign(sp_int = df.Speed.str.replace(' ms', '').astype(int))
  .loc[lambda x: x['sp_int']<1600]
)

Output:

   Port    Speed  Type  sp_int
2  3128   900 ms  HTTP     900
4  8080  1340 ms  HTTP    1340

Upvotes: 1

Related Questions