我想从
['$a', '$b', '$c', '$d', '$e']
to
['a', 'b', 'c', 'd', 'e']
我想从
['$a', '$b', '$c', '$d', '$e']
to
['a', 'b', 'c', 'd', 'e']
当前回答
假设您的数据集名称为df,df具有。
df = ['$a', '$b', '$c', '$d', '$e']`
所以,要重命名这些,我们只需这样做。
df.columns = ['a','b','c','d','e']
其他回答
df.rename(index=str, columns={'A':'a', 'B':'b'})
pandas.DataFrame.rename
只需将其分配给.columns属性:
>>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})
>>> df
$a $b
0 1 10
1 2 20
>>> df.columns = ['a', 'b']
>>> df
a b
0 1 10
1 2 20
可以将lstrip或strip方法与索引一起使用:
df.columns = df.columns.str.lstrip('$')
or
cols = ['$a', '$b', '$c', '$d', '$e']
pd.Series(cols).str.lstrip('$').tolist()
输出:
['a', 'b', 'c', 'd', 'e']
这真的很简单。只需使用:
df.columns = ['Name1', 'Name2', 'Name3'...]
它将按照您输入的顺序分配列名。
# This way it will work
import pandas as pd
# Define a dictionary
rankings = {'test': ['a'],
'odi': ['E'],
't20': ['P']}
# Convert the dictionary into DataFrame
rankings_pd = pd.DataFrame(rankings)
# Before renaming the columns
print(rankings_pd)
rankings_pd.rename(columns = {'test':'TEST'}, inplace = True)