Renaming columns in pandas

前端 未结 27 2544
野性不改
野性不改 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条回答
  •  甜味超标
    2020-11-21 07:33

    In addition to the solution already provided, you can replace all the columns while you are reading the file. We can use names and header=0 to do that.

    First, we create a list of the names that we like to use as our column names:

    import pandas as pd
    
    ufo_cols = ['city', 'color reported', 'shape reported', 'state', 'time']
    ufo.columns = ufo_cols
    
    ufo = pd.read_csv('link to the file you are using', names = ufo_cols, header = 0)
    

    In this case, all the column names will be replaced with the names you have in your list.

提交回复
热议问题