英文:
drop dataframe rows that contain entries being either -inf or NaN
问题
在一个 pandas 数据帧中,如果有条目等于-inf
或NaN
,如何删除所有相关列。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论