Renaming columns in pandas

前端 未结 27 2600
野性不改
野性不改 2020-11-21 07:05

I have a DataFrame using pandas and column labels that I need to edit to replace the original column labels.

I\'d like to change the column names in a DataFrame

相关标签:
27条回答
  • Let's Understand renaming by a small example...

    1.Renaming columns using mapping:

    df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) #creating a df with column name A and B
    df.rename({"A": "new_a", "B": "new_b"},axis='columns',inplace =True) #renaming column A with 'new_a' and B with 'new_b'
    
    output:
       new_a  new_b
    0  1       4
    1  2       5
    2  3       6
    

    2.Renaming index/Row_Name using mapping:

    df.rename({0: "x", 1: "y", 2: "z"},axis='index',inplace =True) #Row name are getting replaced by 'x','y','z'.
    
    output:
           new_a  new_b
        x  1       4
        y  2       5
        z  3       6
    
    0 讨论(0)
  • 2020-11-21 07:14

    Note that these approach do not work for a MultiIndex. For a MultiIndex, you need to do something like the following:

    >>> df = pd.DataFrame({('$a','$x'):[1,2], ('$b','$y'): [3,4], ('e','f'):[5,6]})
    >>> df
       $a $b  e
       $x $y  f
    0  1  3  5
    1  2  4  6
    >>> rename = {('$a','$x'):('a','x'), ('$b','$y'):('b','y')}
    >>> df.columns = pandas.MultiIndex.from_tuples([
            rename.get(item, item) for item in df.columns.tolist()])
    >>> df
       a  b  e
       x  y  f
    0  1  3  5
    1  2  4  6
    
    0 讨论(0)
  • 2020-11-21 07:16
    df.columns = ['a', 'b', 'c', 'd', 'e']
    

    It will replace the existing names with the names you provide, in the order you provide.

    0 讨论(0)
  • 2020-11-21 07:16

    Real simple just use

    df.columns = ['Name1', 'Name2', 'Name3'...]
    

    and it will assign the column names by the order you put them

    0 讨论(0)
  • 2020-11-21 07:18

    Here's a nifty little function I like to use to cut down on typing:

    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   
    

    Here is an example of how it works:

    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')
    
    0 讨论(0)
  • 2020-11-21 07:19

    Another way we could replace the original column labels is by stripping the unwanted characters (here '$') from the original column labels.

    This could have been done by running a for loop over df.columns and appending the stripped columns to df.columns.

    Instead , we can do this neatly in a single statement by using list comprehension like below:

    df.columns = [col.strip('$') for col in df.columns]
    

    (strip method in Python strips the given character from beginning and end of the string.)

    0 讨论(0)
提交回复
热议问题