使用pandas在我的数据框中添加新列

9jyewag0  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(73)

我有一个六列的DataFrame:AgeGenderEducation LevelJob TitleYears of ExperienceSalary。此外,我还有四个技能集列:Programming skillsMicrosoft OfficeWeb developmentSales management,每个技能等级标记为10。我想在DataFrame中添加一个新列,它表示每个人的平均技能水平。
我正在寻找一种方法,根据技能集列中的值计算平均技能水平,并将计算出的平均值作为新列添加到DataFrame中。
下面是我的DataFrame的一个示例和我到目前为止尝试过的代码:

import pandas as pd

data = {
    'Age': [30, 25, 35],
    'Gender': ['Male', 'Female', 'Male'],
    'Education Level': ['Bachelor', 'Master', 'Ph.D.'],
    'Job Title': ['Engineer', 'Manager', 'Analyst'],
    'Years of Experience': [5, 8, 3],
    'Salary': [60000, 80000, 55000],
    'Programming skills': [8, 7, 6],
    'Microsoft Office': [9, 8, 7],
    'Web development': [6, 5, 7],
    'Sales management': [7, 8, 6]
}

df = pd.DataFrame(data)

# Calculate and add the average skill level column here
rjee0c15

rjee0c151#

你可以尝试添加一个新的df或者这两个中的一个:df1.insert(1, "newcol", newvalue)df1['newcol'] = newvalue。你可以这样做:df1.append(df2)

ttp71kqs

ttp71kqs2#

这取决于你要找的是什么,但总的来说,它看起来像这样

df['new_column'] = values

其中values是一个列表或数组,并且必须与您的数组长度相同

omtl5h9j

omtl5h9j3#

你可以在corr的帮助下找到列之间的关系,这样你就有了一些关于你的数据的信息,这样你就可以做你的计算了,但是记住在replace的帮助下将数据为str的列转换为int,然后使用corr,这样就没有错误了,然后你用平均df创建了自己的列。
[“Gender”]=df.[“Gender”].replace([“male”,“famel”],[0,1])

和其余具有str值的列以相同的方式

完成计算后,使用此命令df创建自己的列。

[“average”]=pd.DataFrame([your_average])

相关问题