简体   繁体   中英

unable to add new column in dataframe using python pandas

pretty new to python and using pandas to try and add a new column called "study_id" that references another column called "project_id" & adding "_1" to all the rows, and then saving it in a folder called 07newcolumn. example output below.

project_id study_id (desired output)
asdfj asdfj_1
dfjek dfjek_1

however, the script i'm writing (the part under #add new column) doesn't seem to be working.if i can get any insight as to what im doing wrong that would be greatly appreciated!!

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 = bls_data['study_id'] = bls_data['project_id'] + "_1"
        df = df.to_csv('07newcolumn/' + file[:-5] + 'vF.csv', index=True)

You can try

df['study_id (desired output)'] = df['project_id'].astype(str) + '_1'

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