我想从
['$a', '$b', '$c', '$d', '$e']
to
['a', 'b', 'c', 'd', 'e']
我想从
['$a', '$b', '$c', '$d', '$e']
to
['a', 'b', 'c', 'd', 'e']
当前回答
一个简单而“有趣”(和蟒蛇?)的解决方案:
df.rename(columns={x: x.replace('$', '') for x in df.columns})
哪里:
df = pd.DataFrame(columns=['$a', '$b', '$c', '$d', '$e'])
步骤:
获取DataFrame的列作为列表:
df.columns
在DataFrames中重命名的方法:
df.rename()
属性以指定要重命名列:
columns={}
在字典中,您需要指定要重命名的列(在每个键中)以及它们将获得的新名称(每个值)
{'old_col_name': 'new_col_name', ...}
由于您的更改遵循一种模式,为了删除每列中的$字符,我们可以使用字典理解:
{x: x.replace('$', '') for x in df.columns}
其他回答
Use:
old_names = ['$a', '$b', '$c', '$d', '$e']
new_names = ['a', 'b', 'c', 'd', 'e']
df.rename(columns=dict(zip(old_names, new_names)), inplace=True)
这样,您可以根据需要手动编辑new_names。当您只需要重命名几个列来纠正拼写错误、重音、删除特殊字符等时,它非常有用。
假设您的数据集名称为df,df具有。
df = ['$a', '$b', '$c', '$d', '$e']`
所以,要重命名这些,我们只需这样做。
df.columns = ['a','b','c','d','e']
如果您已经获得了数据帧,df.columns将所有内容转储到您可以操作的列表中,然后作为列的名称重新分配到数据帧中。。。
columns = df.columns
columns = [row.replace("$", "") for row in columns]
df.rename(columns=dict(zip(columns, things)), inplace=True)
df.head() # To validate the output
最佳方式?我不知道。一种方式——是的。
评估问题答案中提出的所有主要技术的更好方法如下:使用cProfile测量内存和执行时间@kadee、@kaitlyn和@eumiro拥有执行时间最快的函数-尽管这些函数非常快,但我们比较了所有答案的0.000和0.001秒舍入。寓意:我上面的答案可能不是“最好”的方式。
import pandas as pd
import cProfile, pstats, re
old_names = ['$a', '$b', '$c', '$d', '$e']
new_names = ['a', 'b', 'c', 'd', 'e']
col_dict = {'$a': 'a', '$b': 'b', '$c': 'c', '$d': 'd', '$e': 'e'}
df = pd.DataFrame({'$a':[1, 2], '$b': [10, 20], '$c': ['bleep', 'blorp'], '$d': [1, 2], '$e': ['texa$', '']})
df.head()
def eumiro(df, nn):
df.columns = nn
# This direct renaming approach is duplicated in methodology in several other answers:
return df
def lexual1(df):
return df.rename(columns=col_dict)
def lexual2(df, col_dict):
return df.rename(columns=col_dict, inplace=True)
def Panda_Master_Hayden(df):
return df.rename(columns=lambda x: x[1:], inplace=True)
def paulo1(df):
return df.rename(columns=lambda x: x.replace('$', ''))
def paulo2(df):
return df.rename(columns=lambda x: x.replace('$', ''), inplace=True)
def migloo(df, on, nn):
return df.rename(columns=dict(zip(on, nn)), inplace=True)
def kadee(df):
return df.columns.str.replace('$', '')
def awo(df):
columns = df.columns
columns = [row.replace("$", "") for row in columns]
return df.rename(columns=dict(zip(columns, '')), inplace=True)
def kaitlyn(df):
df.columns = [col.strip('$') for col in df.columns]
return df
print 'eumiro'
cProfile.run('eumiro(df, new_names)')
print 'lexual1'
cProfile.run('lexual1(df)')
print 'lexual2'
cProfile.run('lexual2(df, col_dict)')
print 'andy hayden'
cProfile.run('Panda_Master_Hayden(df)')
print 'paulo1'
cProfile.run('paulo1(df)')
print 'paulo2'
cProfile.run('paulo2(df)')
print 'migloo'
cProfile.run('migloo(df, old_names, new_names)')
print 'kadee'
cProfile.run('kadee(df)')
print 'awo'
cProfile.run('awo(df)')
print 'kaitlyn'
cProfile.run('kaitlyn(df)')
一个简单而“有趣”(和蟒蛇?)的解决方案:
df.rename(columns={x: x.replace('$', '') for x in df.columns})
哪里:
df = pd.DataFrame(columns=['$a', '$b', '$c', '$d', '$e'])
步骤:
获取DataFrame的列作为列表:
df.columns
在DataFrames中重命名的方法:
df.rename()
属性以指定要重命名列:
columns={}
在字典中,您需要指定要重命名的列(在每个键中)以及它们将获得的新名称(每个值)
{'old_col_name': 'new_col_name', ...}
由于您的更改遵循一种模式,为了删除每列中的$字符,我们可以使用字典理解:
{x: x.replace('$', '') for x in df.columns}
如“使用文本数据:
df.columns = df.columns.str.replace('$', '')