簡體   English   中英

Python - 數據框列在“.”后重命名為大寫字母

[英]Python - data frame columns rename with capital letter after the '.'

我有一些列遵循模式“abc.def”,我正在嘗試使用函數將其更改為“abcDef”。 我可以用df.rename(columns={'abc.def': 'abcDef'}, inplace = True)但尋找一種可以應用於不同數據框的更通用的方法。 我是為簡單的字符串做的,我不知道如何將它應用於列名。 我試圖將列名添加到列表中並將函數附加到列表中,但這也不起作用。

我的 df 是:

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)

我想你想要那樣的東西?

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])))

我使用了@Arne 和@LeMorse 的答案並編譯了我需要的內容。 再次感謝!

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]

您可以將代碼將單個字符串轉換為函數,然后將此函數應用於每個列名,例如使用列表理解:

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]

請注意,目前您的代碼也將第一個字母大寫。

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)

它幾乎與其他答案相似,但它在拆分信號方面更通用,並用注釋清楚地解釋了該過程。 如果您還需要對代碼進行更多評論,請告訴我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM