查找行并删除它 – Pandas DataFrame

huangapple go评论62阅读模式
英文:

Find Rows And Delete it - Pandas DataFrame

问题

这是示例数据帧:

          name stuff  floats  ints
    0     Mike     a     1.0     1
    1     Joey     d     2.2     3
    2  Zendaya     c     NaN     8
    3     John     a     1.0     1
    4  Abruzzi     d     NaN     3

我有一个'to_delete'列表:

[['Abruzzi', 'd', pd.NA, 3], ['Mike', 'a', 1.0, 1]]

我应该使用哪个pandas方法来根据'to_delete'列表删除数据帧中的数据?

这样,我将获得新的数据帧:

          name stuff  floats  ints
    1     Joey     d     2.2     3
    2  Zendaya     c     NaN     8
    3     John     a     1.0     1

谢谢,

*我是pandas的新手

英文:
Example dataframe:
      name stuff  floats  ints
0     Mike     a     1.0     1
1     Joey     d     2.2     3
2  Zendaya     c     NaN     8
3     John     a     1.0     1
4  Abruzzi     d     NaN     3

I have 'to_delete' list :

[['Abruzzi', 'd', pd.NA, 3], ['Mike', 'a', 1.0, 1]]

How can i remove data in the dataframe based on the 'to_delete' list?
What pandas method suit this?

So i will get new dataframe like:

      name stuff  floats  ints
1     Joey     d     2.2     3
2  Zendaya     c     NaN     8
3     John     a     1.0     1

Thanks,

*im new to pandas

答案1

得分: 3

以下是您要翻译的内容:

I would use a merge with indicator:

keep = (
 df.merge(pd.DataFrame(to_delete, columns=df.columns), how='left', indicator=True)
   .query('_merge == "left_only"').index
)

out = df.loc[keep]

print(out)

Output:

      name stuff floats  ints
1     Joey     d    2.2     3
2  Zendaya     c   <NA>     8
3     John     a    1.0     1
英文:

I would use a merge with indicator:

keep = (
 df.merge(pd.DataFrame(to_delete, columns=df.columns), how=&#39;left&#39;, indicator=True)
   .query(&#39;_merge == &quot;left_only&quot;&#39;).index
)

out = df.loc[keep]

print(out)

Output:

      name stuff floats  ints
1     Joey     d    2.2     3
2  Zendaya     c   &lt;NA&gt;     8
3     John     a    1.0     1

答案2

得分: 0

你可以使用drop函数来删除Pandas DataFrame中的行和列。

你可以使用以下代码来查找行并删除:

import pandas as pd

data = pd.DataFrame({
    'name': ['Mike', 'Joey', 'Zendaya', 'John', 'Abruzzi'],
    'stuff': ['a', 'd', 'c', 'a', 'd'],
    'floats': [1.0, 2.2, pd.NA, 1.0, pd.NA],
    'ints': [1, 3, 8, 1, 3]
})

to_remove = [['Abruzzi', 'd', pd.NA, 3], ['Mike', 'a', 1.0, 1]]

data = data[~data.isin(to_remove)].dropna(how='all')
英文:

You can use the drop function to delete rows and columns in a Pandas DataFrame.

You can use the following code for your help finding the row and delete.

import pandas as pa

res = pa.DataFrame({
    &#39;name&#39;: [&#39;Mike&#39;, &#39;Joey&#39;, &#39;Zendaya&#39;, &#39;John&#39;, &#39;Abruzzi&#39;],
    &#39;stuff&#39;: [&#39;a&#39;, &#39;d&#39;, &#39;c&#39;, &#39;a&#39;, &#39;d&#39;],
    &#39;floats&#39;: [1.0, 2.2, pa.NA, 1.0, pa.NA],
    &#39;ints&#39;: [1, 3, 8, 1, 3]
})

remove = [[&#39;Abruzzi&#39;, &#39;d&#39;, pa.NA, 3], [&#39;Mike&#39;, &#39;a&#39;, 1.0, 1]]

res = res[~res.isin(remove)].dropna(how=&#39;all&#39;)

huangapple
  • 本文由 发表于 2023年2月19日 16:24:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定