[英]Drop rows of NaN with a slice of columns in Pandas
I have hundreds of columns in a DataFrame and would like to drop rows where multiple columns are NaN.我在 DataFrame 中有数百列,并且想删除多列为 NaN 的行。 Meaning entire row is NaN for those columns.这意味着这些列的整行都是 NaN。
I have tried to slice columns but the code is taking forever to run.我试图对列进行切片,但代码需要永远运行。
df = df.drop(df[(df.loc[:,'col1':'col100'].isna()) & (df.loc[:,'col120':'col220'].isna())].index)
Appreciate any help.感谢任何帮助。
You should try to use the dropna()
function with the subset
parameter equal to the columns you are trying to drop on.您应该尝试使用dropna()
函数,其subset
参数等于您尝试删除的列。 Here is a short example taken from Pandas' documentation这是 Pandas 文档中的一个简短示例
df = pd.DataFrame({"name": ['Alfred', 'Batman', 'Catwoman'],
"toy": [np.nan, 'Batmobile', 'Bullwhip'],
"born": [pd.NaT, pd.Timestamp("1940-04-25"),
pd.NaT]})
df
name toy born
0 Alfred NaN NaT
1 Batman Batmobile 1940-04-25
2 Catwoman Bullwhip NaT
df.dropna(subset=['name', 'born'])
This gives you the following:这为您提供以下内容:
name toy born
1 Batman Batmobile 1940-04-25
Part of your original question reads: "... would like to drop rows where multiple columns are NaN. Meaning entire row is NaN for those columns. "您的原始问题的一部分是: “... 想要删除多列是 NaN 的行。这意味着这些列的整行都是 NaN。”
Can I interpret this as, you want to delete the row when the entire row has NaNs.我可以将其解释为,当整行都有 NaN 时,您想删除该行。 If that is true you should be able to achive this by:如果这是真的,您应该能够通过以下方式实现这一目标:
df.dropna(axis = 'rows', how = 'all', inplace = True)
If that is not the case then I misunderstood your question.如果不是这样,那么我误解了你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.