我想从
['$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}
其他回答
如“使用文本数据:
df.columns = df.columns.str.replace('$', '')
我的单线回答是
df.columns=df_new_cols
它是最好的,处理时间为1/3。
timeit比较:
df有七列。我正在尝试更改一些名称。
%timeit df.rename(columns={old_col:new_col for (old_col,new_col) in zip(df_old_cols,df_new_cols)},inplace=True)
214 µs ± 10.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit df.rename(columns=dict(zip(df_old_cols,df_new_cols)),inplace=True)
212 µs ± 7.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit df.columns = df_new_cols
72.9 µs ± 17.2 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
如果您只想删除“$”符号,请使用以下代码
df.columns = pd.Series(df.columns.str.replace("$", ""))
这里有一个我喜欢用来减少打字的漂亮小函数:
def rename(data, oldnames, newname):
if type(oldnames) == str: # Input can be a string or list of strings
oldnames = [oldnames] # When renaming multiple columns
newname = [newname] # Make sure you pass the corresponding list of new names
i = 0
for name in oldnames:
oldvar = [c for c in data.columns if name in c]
if len(oldvar) == 0:
raise ValueError("Sorry, couldn't find that column in the dataset")
if len(oldvar) > 1: # Doesn't have to be an exact match
print("Found multiple columns that matched " + str(name) + ": ")
for c in oldvar:
print(str(oldvar.index(c)) + ": " + str(c))
ind = input('Please enter the index of the column you would like to rename: ')
oldvar = oldvar[int(ind)]
if len(oldvar) == 1:
oldvar = oldvar[0]
data = data.rename(columns = {oldvar : newname[i]})
i += 1
return data
下面是一个如何工作的示例:
In [2]: df = pd.DataFrame(np.random.randint(0, 10, size=(10, 4)), columns = ['col1', 'col2', 'omg', 'idk'])
# First list = existing variables
# Second list = new names for those variables
In [3]: df = rename(df, ['col', 'omg'],['first', 'ohmy'])
Found multiple columns that matched col:
0: col1
1: col2
Please enter the index of the column you would like to rename: 0
In [4]: df.columns
Out[5]: Index(['first', 'col2', 'ohmy', 'idk'], dtype='object')
重命名Pandas中的列是一项简单的任务。
df.rename(columns={'$a': 'a', '$b': 'b', '$c': 'c', '$d': 'd', '$e': 'e'}, inplace=True)