Reputation: 515
I have Pandas Dataframe that comes from Excel import:
Trade Symbol Type Signal Date Price Contracts Profit (¤) Run-up (¤) Drawdown (¤)
0 1 FOXA EntryLong Buy 2000-10-18 34.69 200 0 0 0
1 NaN NaN ExitLong Sell 2000-10-18 34.69 200 NaN NaN NaN
2 2 TXN EntryLong Buy 2000-10-18 36.88 200 1774 1900 0
3 NaN NaN ExitLong Sell 2000-10-19 45.75 200 NaN NaN NaN
I need to rename "Profit (¤)" to "Profit" but cannot get rid of the "¤" sign.
I am using:
trades.rename(columns={'Trade #':'Trade','Symbol Name':'Symbol','Profit (¤)':'Profit','Run-up (¤)':'Runup','Drawdown (¤)':'Drawdown'}, inplace=True)
which does not work.
list(trades)
gets:
['Trade',
'Symbol',
u'Type',
u'Signal',
u'Date',
u'Price',
u'Contracts',
u'Profit (\xa4)',
u'Run-up (\xa4)',
u'Drawdown (\xa4)']
Any tip how to rename the colums with the special character? Thank you.
Upvotes: 1
Views: 777
Reputation: 394101
It looks like all you're doing is just dropping the erroneous additional character, as it's delimited by a space you can just overwrite the column names by splitting on the space:
In [104]:
df = pd.DataFrame(columns=['Trade',
'Symbol',
u'Type',
u'Signal',
u'Date',
u'Price',
u'Contracts',
u'Profit (\xa4)',
u'Run-up (\xa4)',
u'Drawdown (\xa4)'])
df
Out[104]:
Empty DataFrame
Columns: [Trade, Symbol, Type, Signal, Date, Price, Contracts, Profit (¤), Run-up (¤), Drawdown (¤)]
Index: []
In [105]:
df.columns.str.split().str[0]
Out[105]:
Index(['Trade', 'Symbol', 'Type', 'Signal', 'Date', 'Price', 'Contracts',
'Profit', 'Run-up', 'Drawdown'],
dtype='object')
So the following should work for you:
trades.columns = trades.columns.str.split().str[0]
Upvotes: 2