-3
energy['Country'] = energy['Country'].str.replace(r" \(.*\)","")

I specifically do not understand what this part is doing -- str.replace(r" \(.*\)","")

Please let me know. Thanks!

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91

1 Answers1

0

First, = energy['Country'] is getting the value from energy that corresponds to 'Country'.

This piece could be separated as follow:

obj = energy['Country']
energy['Country'] = obj.str.replace(r" \(.*\)","")

Next, the attribute str is accessed from obj. Again separating that out:

obj = energy['Country']
attr = obj.str
energy['Country'] = attr.replace(r" \(.*\)","")

And then the function replace() is called off of the value str.

Note, the value str here is not the same as the builtin class str. Instead it is an attribute of the class that is returned from energy['Country'].

The Matt
  • 1,423
  • 1
  • 12
  • 22