0

I think this is a really easy question but I just wanted to clarify.

Do you have to continuously rename a dataframe when changing it?

df18gdp = df18[df18.series=='GDP (current US$)']
df18gdp = df18gdp.drop(columns = 'series')
df18gdp = df18gdp.set_index('country')
df18gdp = df18gdp.drop(index = ['World', 'Low income', 'Middle income', 'High income'])
df18gdp = df18gdp.sort_values('2018', ascending=False)

Could I simply do the following?

Is there a smoother way of making changes like this??

Constantly saying 'df18gdp=' seems redundant

df18gdp = df18[df18.series=='GDP (current US$)']
   df18gdp.drop(columns = 'series')
   df18gdp.set_index('country')
   df18gdp.drop(index = ['World', 'Low income', 'Middle income', 'High income'])
   df18gdp.sort_values('2018', ascending=False)

Thank you

Community
  • 1
  • 1
Alex
  • 188
  • 11
  • 1
    `inplace=True`. That parameter is literally everywhere in the documentation. You either do a reassignment back as you do in your first example, or use `inplace=True` to do the second example – roganjosh Aug 21 '19 at 18:15
  • Also see: [Pandas - is inplace = True considered harmful or not?](https://stackoverflow.com/questions/45570984/pandas-is-inplace-true-considered-harmful-or-not) – roganjosh Aug 21 '19 at 18:21
  • I saw a similar question in that Python Pandas - Understanding inplace=True, but from what I read, I could not tell if it clarified whether you could use inpalce=True/False for multiple changes, or what that would look like. Their examples only seemed to clarify whether it would or would not create a new variable. Unless I'm missing something(?) – Alex Aug 21 '19 at 18:24
  • Your second example would be equivalent to the first if you used `inplace` unless there's some corner case I'm overlooking. The multiple changes in your example each happen sequentially, so it wouldn't make a difference. If you were to try chain them, that would be different, but you don't even slightly touch on that and perhaps it throws you off-course for now – roganjosh Aug 21 '19 at 18:29

0 Answers0