删除包含条目为 -inf 或 NaN 的数据帧行。

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

drop dataframe rows that contain entries being either -inf or NaN

问题

在一个 pandas 数据帧中,如果有条目等于-infNaN,如何删除所有相关列。

英文:

In a pandas data frame, if there have entries being equal to -inf or NaN, how to remove all the related columns.

答案1

得分: 0

If it doesn't matter that you retain other null-like values (e.g., None, NaT), then replace -inf with nan and drop columns with any null values (default value of how arg of dropna is "any"):

import numpy as np
import pandas as pd

df = pd.DataFrame({
    'a': [1, 2, np.nan],
    'b': [1, np.nan, 3],
    'c': [1, 2, 3]
})
df = df.replace({-np.inf: np.nan}).dropna(axis=1)
df

Output:

   c
0  1
1  2
2  3

Otherwise, I'd do something like this:

import numpy as np
import pandas as pd

TARGET_VALUES = [-np.inf, np.nan]

df = pd.DataFrame({
    'a': [1, 2, np.nan],
    'b': [1, -np.inf, 3],
    'c': ['a', 'b', None]
})
column_value_counts = df.isin(TARGET_VALUES).sum(axis=0)
df = df[column_value_counts[column_value_counts == 0].index.values].copy()
df

Output:

   c
0  a
1  b
2  None
英文:

If it doesn't matter that you retain other null like values (e.g. None, NaT) then replace -inf with nan and drop columns with any null values (default value of how arg of dropna is "any")

import numpy as np
import pandas as pd

df = pd.DataFrame({
    'a': [1,2,np.nan],
    'b': [1,-np.inf,3],
    'c': [1,2,3]
})
df = df.replace({-np.inf: np.nan}).dropna(axis=1)
df

Output:

	c
0	1
1	2
2	3

Otherwise I'd do something like this:

import numpy as np
import pandas as pd

TARGET_VALUES = [-np.inf, np.nan]

df = pd.DataFrame({
    'a': [1,2,np.nan],
    'b': [1,-np.inf,3],
    'c': ['a','b',None]
})
column_value_counts = df.isin(TARGET_VALUES).sum(axis=0)
df = df[column_value_counts[column_value_counts == 0].index.values].copy()
df

Output:

	c
0	a
1	b
2	None

huangapple
  • 本文由 发表于 2023年3月1日 12:13:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599503.html
匿名

发表评论

匿名网友

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

确定