I have a list with many nams of columns that i work with for my project.
my list is like this:
list_colunm = ['solar [W].1', 'Wind [W].02', 'Caz [W].33']
(and other elements it's a long list).
if you can help me with same methods to delete .1 .02 and .33
CodePudding user response:
Standard Python:
list_column = ['solar [W].1', 'Wind [W].02', 'Caz [W].33']
list_shortnames = [x.rsplit('.')[0] for x in list_column]
Output:
['solar [W]', 'Wind [W]', 'Caz [W]']
Pandas:
The most simple way is using rename()
with a dict as a map.
import pandas as pd
mapping = {"solar [W].1": "solar [W]", "Wind [W].02": "Wind [W]", "Caz [W].33": "Caz [W]"}
df.rename(mapping, inplace=True, axis='columns')
More flexible alternative (@mozway):
df.rename(columns=lambda x: x.rsplit('.', n=1)[0])
Output:
solar [W] Wind [W] Caz [W]
0 1 2 3