Skip to content Skip to sidebar Skip to footer

Add Prefix Failed With Percentage

df = pd.DataFrame({'a':[1,4], 'b':[7,8]}) print (df) a b 0 1 7 1 4 8 I try add % to columns names, so use DataFrame.add_prefix print (df.add_prefix('%')) TypeError: not

Solution 1:

NOTE: This is likely to change in the near future as pandas will use new style string formatting in this case. When that happens:

'{}hello'.format('%')

'%hello'

adding a prefix with a single % will work just fine.

See github


Answer Two percent signs! One escapes the other when using the old style of string formatting.

df.add_prefix('%%')

   %a  %b017148

The clue came from:

2856defadd_prefix(self, prefix):
   2857         f = (str(prefix) + '%s').__mod__
-> 2858returnself.rename_axis(f, axis=0)
   28592860defadd_suffix(self, suffix):

So I tried it myself

f = ('my_prefix_' + '%s').__mod__
f('hello')'my_prefix_hello'

And then

f = ('%' + '%s').__mod__
f('hello')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent calllast)
<ipython-input-901-0c92e5498bbc>in<module>()
      1 f = ('%'+'%s').__mod__
----> 2 f('hello')

TypeError: notall arguments converted during string formatting

So I looked up how to escape the '%' in the old style of string formatting and found this answer

Which led to this

f = ('%%' + '%s').__mod__
f('hello')'%hello'

Post a Comment for "Add Prefix Failed With Percentage"