How to remove header names from each rows in Pandas Dataframe

Viewed 18

I'm reading an Html file using Pandas data-frame. Everything going well - the only problem I'm facing is, that all the header's names are getting appended to each row. Please tell me why is this happening and how I can resolve it. Thank you!

enter image description here

1 Answers

You can use:

>>> df
          THUMBNAIL     NAME
0  THUMBNAILxyz.png  NAMExyz
1  THUMBNAILabc.png  NAMEabc

# Solution 1
>>> df.apply(lambda x: x.str[len(x.name):])
  THUMBNAIL NAME
0   xyz.png  xyz
1   abc.png  abc

# Solution 2
>>> df.apply(lambda x: x.str.lstrip(x.name))
  THUMBNAIL NAME
0   xyz.png  xyz
1   abc.png  abc

# Solution 3
>>> df.apply(lambda x: x.str.replace(x.name, ''))
  THUMBNAIL NAME
0   xyz.png  xyz
1   abc.png  abc

# Solution 4
>>> df.apply(lambda x: x.str.extract(fr"{x.name}(.*)", expand=False))
  THUMBNAIL NAME
0   xyz.png  xyz
1   abc.png  abc

And so on.

Related