如果lambda返回None Pandas

时间:2017-11-03 12:49:49

标签: python pandas

self.df['X'] = self.df['x'].apply(lambda x: my_map.get(x))

如何删除my_map.get(x)返回None的行。

我正在寻找一种解决方案,我不必再次遍历列以删除行。

由于

4 个答案:

答案 0 :(得分:4)

我认为您需要dropna,因为可以在第一步中删除foreach (CustomTextCell textCell in parentSection) { System.Diagnostics.Debug.WriteLine($"{textCell.Text} - {textCell.IsChecked}"); } ,通过分配给新列创建None s:

NaN

或者:

self.df['X'] = self.df['x'].apply(lambda x: my_map.get(x))
self.df = self.df.dropna('X')

答案 1 :(得分:2)

public ActionResult Edit(int id) { // didnt make sense // getIssue.item = getIssue.items[id - 1]; Issue getIssue = IssueReposiroty.GetIssueById(id) // exaqmple as no idea hgow retrieved return View(getIssue); } [HttpPost] public ActionResult Edit(Issue issue) { If(ModelState.IsValid) { // process saving of updated item // can pass id or pass model to save retrieving again return RedirectToAction("IssueItem", issue); } else { ModelState.AddModelError(string.Empty, "Please make sure you have filled in all required fields."); } return View(issue); } public ActionResult IssueItem(Issue issue) { return View(issue); } loc获取可调用参数并返回可调用计算结果为pd.Series.compress的子集

<强> True

compress

<强> self.df['x'].compress(lambda x: my_map.get(x) is not None)

loc

答案 2 :(得分:1)

您可以按以下方式找到索引

idxs = self.df.index[self.df['X'].isnull()]  # find all indices with None in df.X

完整代码:

self.df['X'] = self.df['x'].apply(lambda x: my_map.get(x))
idxs = self.df.index[self.df['X'].isnull()]  # find all indices with None in df.X
self.df = self.df.drop(idxs)

答案 3 :(得分:0)

如果您将mymap转换为词典:

,则可以将此作为合并执行
mymerge = pd.DataFrame.from_dict(mymap, orient = 'index')

然后使用左连接,仅加入所需的列:

mymerge.merge(df, left_index = True, right_on = 'x')

在一行中:

pd.DataFrame.from_dict(mymap, orient = 'index').merge(df, left_index = True, right_on = 'x')