Reputation: 366
I have a dataframe which is loaded from a .csv, and I would like to remove some text in the labels.
Right now, my dataframe save the labels as output.text.user.12, output.text.user.1224,... I would like to remove from that labels the part "output.text.user."
output.text.user.12 ... output.text.user.23424
index ...
332 0.06924 ... 0.0
Does anyone know how could I do it? I've seen how to replace the current name from a dictionary, but it's a too long dataframe to do it.
Upvotes: 1
Views: 1378
Reputation: 1366
Consider the following snippet:
import pandas as pd
# your dataframe
df = pd.DataFrame()
# loop over columns, split by dot (.) and select last item in resulting list
new_columns = []
for column in df.columns:
new_columns.append(column.split('.')[-1])
# assign new column names to your dataframe by overwriting the old ones
df.columns = new_columns
Upvotes: 0
Reputation: 149115
DataFrame.rename
is what you want. Assuming your dataframe is df
df = df.rename(columns=lambda x: x.replace('output.text.user.', ''))
Upvotes: 1