I have some columns that follow the pattern 'abc.def' and I'm trying to change it to 'abcDef' with a function. I can do it with df.rename(columns={'abc.def': 'abcDef'}, inplace = True)
but looking for a more generic approach that can be applied to different data frames. I did it for the simple string and I do not know how to apply it to the column names. I have tried to get column names to the list and append the function to the list but that did not work either.
My df is:
import pandas as pd
import re
data = {'end.date': ['01/10/2020 15:23', '01/10/2020 16:31', '01/10/2020 16:20', '01/10/2020 11:00'],
'start.date': ['01/10/2020 13:38', '01/10/2020 14:49', '01/10/2020 14:30','01/10/2020 14:30']
}
df = pd.DataFrame(data, columns = ['end.Date','start.date'])
# below is my go at the text.
text = 'abs.d'
splitFilter = re.compile('([.!?]\s*)')
splitColumnName = splitFilter.split(text)
print(splitColumnName)
final = ''.join([i.capitalize() for i in splitColumnName])
final = final.replace('.', '')
print(final)
I think you want something like that ?
import pandas as pd
import re
data = {'end.date': ['01/10/2020 15:23', '01/10/2020 16:31', '01/10/2020 16:20', '01/10/2020 11:00'],
'start.date': ['01/10/2020 13:38', '01/10/2020 14:49', '01/10/2020 14:30','01/10/2020 14:30']
}
df = pd.DataFrame(data, columns = ['end.Date','start.date'])
# below is my go at the text.
def formatColumn(column) :
splitFilter = re.compile('([.!?]\s*)')
splitColumnName = splitFilter.split(column)
final = ''.join([i.capitalize() for i in splitColumnName])
final = final.replace('.', '')
return final[0].lower() + final[1:]
df.rename(columns=dict(zip(df.columns, [formatColumn(c) for c in df.columns])))
I used the answers from @Arne and from @LeMorse and compiled what I needed. Thanks again!
import pandas as pd
import re
data = {'end.date': ['01/10/2020 15:23', '01/10/2020 16:31', '01/10/2020 16:20', '01/10/2020 11:00'],
'start.date': ['01/10/2020 13:38', '01/10/2020 14:49', '01/10/2020 14:30','01/10/2020 14:30']
}
df = pd.DataFrame(data, columns = ['end.Date','start.date'])
# below is my go at the text.
def formatColumn(column) :
splitFilter = re.compile('([.!?]\s*)')
splitColumnName = splitFilter.split(column)
final = ''.join([i.capitalize() for i in splitColumnName])
final = final.replace('.', '')
return final[0].lower() + final[1:]
df.columns = [formatColumn(col) for col in df.columns]
You could put your code to transform an individual string into a function and then apply this function to every column name, eg with a list comprehension:
def camelCase(text):
splitFilter = re.compile('([.!?]\s*)')
splitColumnName = splitFilter.split(text)
final = ''.join([i.capitalize() for i in splitColumnName])
final = final.replace('.', '')
return final
df.columns = [camelCase(col) for col in df.columns]
Note that currently your code capitalizes the first letter too.
def splitAndRenameColumns(df, splitSignal):
# get all the columns in a list
columnNameList = df.columns.values.tolist()
# create a map to rename columns
# mapping is old.columnname : newColumnname
newColNames = {}
# loop pver all column names
for clm in columnNameList :
# split the column names on "provided split signal i.e dot in this case"
tempStore = clm.split(splitSignal)
# store the first word before dot in temparory string
newString = tempStore[0]
# loop over all other string values we got after splitting
for index in range(1,len(tempStore)):
# capitalise first character to upper case and concatenate all the strings
newString += tempStore[index][0].upper()+tempStore[index][1:]
# create the mapping
# i.e {'end.Date.gate': 'endDateGate', 'start.date.bate': 'startDateBate'}
newColNames[clm] = newString
return newColNames
df = df.rename(columns=splitAndRenameColumns(df, "."))
print(df)
It's almost similar to other answers but It's more generic in-terms of split signals and explains the process with comments clearly. Let me know if you still need more comments on the code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.