how to add a new column to dataframe using pandas - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: News and Discussions (https://python-forum.io/forum-31.html) +--- Thread: how to add a new column to dataframe using pandas (/thread-38332.html) |
how to add a new column to dataframe using pandas - truffleoil - Sep-29-2022 My script below is returning the error 'NoneType' and i can't figure out why. import pandas as pd from os import listdir from os.path import isfile, join onlyfiles = [f for f in listdir('.') if isfile(join('.', f))] print(onlyfiles) #add new column for file in onlyfiles: if file[:1] != '.' and file[-5:] != 'ipynb': print(file) bls_data = pd.read_csv(file) df['study_id'] = df['project_id'].astype(str) + '_1' df = df.to_csv('07newcolumn/' + file[:-5] + 'vF.csv', index=True)Im essentially looking to create a new column called "study_id" that takes an existing column (project_id) and adds "_1" at the end of each row value. So for example right beside project_id, the script would create a new new column called study_id that adds "_1" at the end of each row value. project_id abcd jklm NEW COLUMN study_id abcd_1 jklm_1 Any help is appreciated thank you! |