从两个类别列pandas中创建一个类别列

时间:2018-04-12 14:49:27

标签: python pandas numpy

我有这个数据框

d = {
    'Primary area': [
        'Biological Sciences A',
        'Cultures and Cultural Production',
        'Mathematics'
    ],
    'Discipline': [
        'Biochemistry and Molecular Biology',
        'Philosophy',
        'Pure Mathematics'
    ]
}

import pandas as pd

df = pd.DataFrame(data=d)

    Discipline                              Primary area
0   Biochemistry and Molecular Biology      Biological Sciences A
1   Philosophy                              Cultures and Cultural Production
2   Pure Mathematics                        Mathematics

我想获取一个新列“Mydisciplines”列,该列取决于行从“Discipline”或“Primary area”获取项目。我有一个单词列表

Mydisciplines = ['Biological Sciences A', 'Mathematics', 'Philosophy']

我希望使用这个新列表来过滤两列,然后合并剩下的内容。如

    Discipline                           Mydisciplines          Primary area
0   Biochemistry and Molecular Biology   Biological Sciences A  Biological Sciences A
1   Philosophy                           Philosophy             Cultures and Cultural Production
2   Pure Mathematics                     Mathematics            Mathematics

我尝试了一些东西但是我无法在一段代码中合成我想要的东西。 我很想知道如何解决这个问题。

2 个答案:

答案 0 :(得分:1)

我设法做的就是这个。

创建一个列表,其中包含与主要区域关联的所有学科:

Biological_A = df[(df["Primary area"] == 'Biological Sciences A')].Discipline.unique()
Mathematics = df[(df["Primary area"] == 'Mathematics')].Discipline.unique()

然后替换该列表中的Discipline列中的值:

for x in Biology_A: df.replace({'Discipline': {x:'Biological Sciences A'}}, regex=True, inplace=True) for x in Mathematics: df.replace({'Discipline': {x:'Mathematics'}}, regex=True, inplace=True)

根据需要对其他主要区域重复此操作。

此代码需要

    Discipline                              Primary area
0   Biochemistry and Molecular Biology      Biological Sciences A
1   Philosophy                              Cultures and Cultural Production
2   Pure Mathematics                        Mathematics

    Discipline                              Primary area
0   Biological Sciences A                   Biological Sciences A
1   Philosophy                              Cultures and Cultural Production
2   Pure Mathematics                        Mathematics

它没有真正回答这个问题,因为它没有创建新专栏,但这正是我所需要的,尽管问题的措辞。

答案 1 :(得分:1)

我认为需要extract加上 public ActionResult UserGrid(int PageIndex = 1, string filter = "", string SortColumnName = "", string Sort = "asc", bool IsClient = true) { int pageSize = Common.PageSize(); UserServiceReference.UserServiceClient UserService = new UserServiceReference.UserServiceClient(); if (Sort != "N") { if (_Usersortcolumn == SortColumnName && _usercounter == 0) _sortcolumn = SortColumnName + "_desc"; else _sortcolumn = SortColumnName + "_asc"; } if (SortColumnName != "") _usercounter++; if (_usercounter >= 2) _usercounter = 0; ViewBag.Search = filter.Trim(); _Usersortcolumn = SortColumnName; ViewBag.Sort = SortColumnName; if (ViewBag.IsClient != null) ViewBag.IsClient = IsClient; else ViewBag.IsClient = IsClient; var result = UserService.UserList(filter.Trim(), _sortcolumn, Sort, IsClient); IPagedList<UserListModel> UserPageList = null; List<UserListModel> _UserDataModal = new List<UserListModel>(); foreach (var u in result) { UserListModel ObjUser = new UserListModel(); ObjUser.ID = u.Id; ObjUser.UserName = u.UserName; ObjUser.Name = u.Name; ObjUser.DepartmentName = u.DepartmentName; ObjUser.RoleName = u.RoleName; ObjUser.Nets = u.Nets; _UserDataModal.Add(ObjUser); } UserPageList = _UserDataModal.ToPagedList(PageIndex, pageSize); if (Request.IsAjaxRequest() == true) { return PartialView("~/Areas/Admin/Views/Setting/Partial/_UserGrid.cshtml", UserPageList); } else { return View("~/Areas/Admin/Views/Setting/Partial/_UserGrid.cshtml", UserPageList); } } <a href="#" onclick="UserAddEdit.LoadUser('name','desc')">Name</a><span><i id="name" class=""></i></span> 的所有值,用于正则表达式Mydisciplines|的字边界:

OR