简体   繁体   中英

Error when adding a new column to pandas dataframe

I am trying to modify .csv files in a folder. The files contain flight information from years 2011-2016.

However, year information cannot be found in the values.

I would like to solve this by using the filename of the .csv file which contains the year. I am adding a new 'year' column after reading it into a pandas dataframe. I will then export the modified file to a new .csv with only the year as its filename.

However, I am encountering this error:

ValueError:Length of values does not match length of index

Code below for your reference.

import pandas as pd
import glob
import re
import os

path = r'data_caap/'                   
all_files = glob.glob(os.path.join(path, "*.csv"))


for f in all_files:
    df = pd.read_csv(f)
    year= re.findall(r'\d{4}', f)

    #Error here
    df['year']=year
    #Error here

    df.to_csv(year)

Found the cause of the error.

Must be df['year']=year[0]. findall returns a list. – DyZ

Thanks a lot @Dyz

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM