python 空白行_如何忽略空白值存在的行Pandas Python

在使用Pandas DataFrame时,遇到因空值导致筛选结果为空的问题。原始DataFrame中存在空值,当尝试根据指定条件筛选时,结果为空。通过删除Excel中所有空值行后,筛选操作正常。解决方案可能是使用`dropna()`方法删除含有空值的行,或者在筛选时使用`loc`来确保正确过滤。
摘要由CSDN通过智能技术生成

What i'm trying to do is query a Panda DataFrame in order to give me a filtered version of the original one

self.waferInfo = pd.read_csv(fileName, index_col= None, na_values=['NA', ""] , usecols=[18,5,6,8,2])

print(self.waferInfo.head(5))

self.df2 = self.waferInfo[(self.waferInfo.FILE_FINISH_TS >= dateBegin) & (self.waferInfo.FILE_FINISH_TS <= dateEnd) ]

print(self.df2.head(5))

when the first print happens the expected rows print out but when the 2nd one is called, it appears empty. I figured out the reason that was happening was because the original DataFrame has some blanks

for example :

18 5 6 8 2

A B C E

D E T Y P

F R B A L

I would want my Dataframe to return

18 5 6 8 2

D E T Y P

F R B A L

the fact that Column 8 has an empty cell it returns a complete empty DataFrame. I know this because I deleted all the rows that had empty cell's in excel and the DataFrame worked fine after that.

is there any way to ignore rows that have a missing value.

解决方案

I do not think that your assumptions about the root cause of the problem are correct. See below.

"""

18 5 6 8 2

A B C E

D E T Y P

F R B A L

"""

import pandas as pd

import numpy as np

df = pd.read_clipboard()

print(df)

print("\n")

print(df.dropna())

Output:

18 5 6 8 2

0 A B C E None

1 D E T Y P

2 F R B A L

18 5 6 8 2

1 D E T Y P

2 F R B A L

If df2.head(5) returns nothing, then it's because df2 is empty, which is not because there are NaN's in your df.

Perhaps

self.waferInfo[(self.waferInfo.FILE_FINISH_TS >= dateBegin) & \

(self.waferInfo.FILE_FINISH_TS <= dateEnd) ]

should be

self.waferInfo.loc[(self.waferInfo.FILE_FINISH_TS >= dateBegin) & \

(self.waferInfo.FILE_FINISH_TS <= dateEnd) ]

I can't say for sure because you haven't provided enough sample data.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值