Key error when selecting columns in pandas dataframe after read_csv

后端 未结 3 2120
一向
一向 2020-12-01 10:49

I\'m trying to read in a CSV file into a pandas dataframe and select a column, but keep getting a key error.

The file reads in successfully and I can view the datafr

3条回答
  •  春和景丽
    2020-12-01 11:21

    The key error generally comes if the key doesn't match any of the dataframe column name 'exactly':

    You could also try:

    import csv
    import pandas as pd
    import re
        with open (filename, "r") as file:
            df = pd.read_csv(file, delimiter = ",")
            df.columns = ((df.columns.str).replace("^ ","")).str.replace(" $","")
            print(df.columns)
    

提交回复
热议问题