Python Forum
how to add a new column to dataframe using pandas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to add a new column to dataframe using pandas
#1
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!
Larz60+ write Sep-30-2022, 02:34 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020