如何循环将新列添加到具有值的pandas数据框基于其他列值

时间:2019-07-02 13:54:30

标签: python regex pandas

请给我一个包含四列的数据框,它们分别是[键,摘要,描述和summary_description],所以我要处理summary_description,尝试应用RegEx并将新结果提取到[New_column]中,所以我完成了循环但我不确定是什么问题导致无法正常工作。如果有人可以帮忙,请多多关照。

     fetch('users.json')
        .then(res=>res.json())
        .then(users=>{
        users.forEach(user=>{
            out+=`
            <div id='module'>
            <h3>Name: ${user.name}<h3>
            <h3>Email: ${user.email}<h3>
            <h3>Number: ${user.number}<h3>
            </div>`
        })
        parentDiv.innerHTML=out;
        })

错误:

import pandas as pd
import re
dataf= pd.read_excel(r'C:\Users\malotaibi\Desktop\Last update\result.xlsx')
dataf
dataf.head(5)
dataf['New_Column'][i] = re.sub('[^A-Za-z0-9]+', ' ', dataf['Summary_Description'][i])
print (dataf['New_column'][i])

2 个答案:

答案 0 :(得分:1)

您可以这样做:

dataf['New_Column'] = dataf['Summary_Description'].str.replace('[^A-Za-z0-9]+', ' ')

答案 1 :(得分:0)

您已尝试在创建新列之前将其添加。

import pandas as pd
import re
dataf= pd.read_excel(r'C:\Users\malotaibi\Desktop\Last update\result.xlsx')
dataf
dataf.head(5)
dataf['New_Column'] = 1 # this will create the new_column entry and set all its values to 1

您现在可以循环浏览并将每个值设置为所需的值。我假设您要使用类似的东西:

for i in range(len(dataf['Summary_Description'])):
    dataf['New_Column'][i] = re.sub('[^A-Za-z0-9]+', ' ', dataf['Summary_Description'][i])