查找行并删除它 – Pandas DataFrame

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

Find Rows And Delete it - Pandas DataFrame

问题

这是示例数据帧:

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

我有一个'to_delete'列表:

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

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

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

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

谢谢,

*我是pandas的新手

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

I have 'to_delete' list :

  1. [['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:

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

Thanks,

*im new to pandas

答案1

得分: 3

以下是您要翻译的内容:

I would use a merge with indicator:

  1. keep = (
  2. df.merge(pd.DataFrame(to_delete, columns=df.columns), how='left', indicator=True)
  3. .query('_merge == "left_only"').index
  4. )
  5. out = df.loc[keep]
  6. print(out)

Output:

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

I would use a merge with indicator:

  1. keep = (
  2. df.merge(pd.DataFrame(to_delete, columns=df.columns), how=&#39;left&#39;, indicator=True)
  3. .query(&#39;_merge == &quot;left_only&quot;&#39;).index
  4. )
  5. out = df.loc[keep]
  6. print(out)

Output:

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

答案2

得分: 0

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

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

  1. import pandas as pd
  2. data = pd.DataFrame({
  3. 'name': ['Mike', 'Joey', 'Zendaya', 'John', 'Abruzzi'],
  4. 'stuff': ['a', 'd', 'c', 'a', 'd'],
  5. 'floats': [1.0, 2.2, pd.NA, 1.0, pd.NA],
  6. 'ints': [1, 3, 8, 1, 3]
  7. })
  8. to_remove = [['Abruzzi', 'd', pd.NA, 3], ['Mike', 'a', 1.0, 1]]
  9. 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.

  1. import pandas as pa
  2. res = pa.DataFrame({
  3. &#39;name&#39;: [&#39;Mike&#39;, &#39;Joey&#39;, &#39;Zendaya&#39;, &#39;John&#39;, &#39;Abruzzi&#39;],
  4. &#39;stuff&#39;: [&#39;a&#39;, &#39;d&#39;, &#39;c&#39;, &#39;a&#39;, &#39;d&#39;],
  5. &#39;floats&#39;: [1.0, 2.2, pa.NA, 1.0, pa.NA],
  6. &#39;ints&#39;: [1, 3, 8, 1, 3]
  7. })
  8. remove = [[&#39;Abruzzi&#39;, &#39;d&#39;, pa.NA, 3], [&#39;Mike&#39;, &#39;a&#39;, 1.0, 1]]
  9. 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:

确定