英文:
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='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
答案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({
'name': ['Mike', 'Joey', 'Zendaya', 'John', 'Abruzzi'],
'stuff': ['a', 'd', 'c', 'a', 'd'],
'floats': [1.0, 2.2, pa.NA, 1.0, pa.NA],
'ints': [1, 3, 8, 1, 3]
})
remove = [['Abruzzi', 'd', pa.NA, 3], ['Mike', 'a', 1.0, 1]]
res = res[~res.isin(remove)].dropna(how='all')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论