To add a prefix/suffix to a dataframe, I usually do the following..
For instance, to add a suffix '@'
,
df = df.astype(str) + '@'
This has basically appended a '@'
to all cell values.
I would like to know how to remove this suffix. Is there a method available with the pandas.DataFrame class directly that removes a particular prefix/suffix character from the entire DataFrame ?
I've tried iterating through the rows (as series) while using rstrip('@')
as follows:
for index in range(df.shape[0]):
row = df.iloc[index]
row = row.str.rstrip('@')
Now, in order to make dataframe out of this series,
new_df = pd.DataFrame(columns=list(df))
new_df = new_df.append(row)
However, this doesn't work. Gives empty dataframe.
Is there something really basic that I am missing?
You could use applymap to apply your string method to each element:
Note: I wouldn't expect this to be as fast as the vectorized approach:
pd.Series.str.rstrip
i.e. transforming each column separately