簡體   English   中英

無法使用 python 在 dataframe 中添加新列 pandas

[英]unable to add new column in dataframe using python pandas

python 非常新,並使用 pandas 嘗試添加一個名為“study_id”的新列,該列引用另一個名為“project_id”的列並將“_1”添加到所有行,然后將其保存在名為 07newcolumn 的文件夾中。 下面的示例 output。

項目編號 study_id(期望的輸出)
asdfj asdfj_1
dfjek dfjek_1

但是,我正在編寫的腳本(#add new column 下的部分)似乎不起作用。如果我能深入了解我做錯了什么,將不勝感激!

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)

你可以試試

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM