简体   繁体   English

如何消除熊猫中的行

[英]How to eliminate rows in Pandas

this is a beginner question, but I could not find the answer to this particular problem anywhere. 这是一个初学者的问题,但是我在任何地方都找不到这个特定问题的答案。

I am trying to graph two values using matplotlib, I have have a CSV table, and I am using Pandas to manipulate it. 我正在尝试使用matplotlib绘制两个值的图形,我有一个CSV表,并且正在使用Pandas对其进行操作。 When trying to graph I get "Key error, not in index". 尝试绘制图形时出现“键错误,不在索引中”。

This is because some the values in my table are missing. 这是因为表中的某些值丢失了。 So I say 所以我说

df = df.dropna()

I have two values I am trying to graph v_d and v_a. 我有两个值正在尝试绘制v_d和v_a。 The problem is that some of the "N/A" or missing values may be missing from the v_a row but not from v_d row. 问题是v_a行中可能缺少某些“ N / A”或缺少值,但v_d行中却没有。

I do not know how to completely delete a row if any of the values are missing, and then rearrange my df so that there are no holes or gaps. 我不知道如何在缺少任何值的情况下完全删除一行,然后重新排列我的df,以便没有孔或间隙。 Basically to remove all rows that have any missing data in v_d or v_a, and then shuffle then slide the remaining rows to take their spots. 基本上是删除v_d或v_a中所有缺少数据的所有行,然后随机播放然后滑动其余行以占据其位置。

I believe this would prevent me from getting the key error. 我相信这将阻止我得到关键错误。

EDIT: for more information in response to my question 编辑:有关我的问题的更多信息

Here is specifically what I am trying to plot 这是我要绘制的内容

df.plot.scatter(x = df.v_d[df.V > 0.1], xlim=[-500,500] , y = df.v_a[df.V > 0.1] , ylim=[-500,500])

where V is another quantity I defined 其中V是我定义的另一个量

When I try to plot this I get a key error not in index - I was assuming it was because some of my values of v_d and v_a are n/a 当我尝试绘制此图形时,出现了一个关键错误,而不是索引错误-我以为是因为我的v_d和v_a的某些值不适用

What I really wanted to know, and could not find in the documentation for drop.na() is how to drop a row completely if any value in the row is null. 我真正想知道的是,在drop.na()的文档中找不到的是如何在行中的任何值为null的情况下完全删除行。

I figured out what I needed to do 我知道我需要做什么

df = df[np.isfinite(df['v_d'])]

etc ... 等...

posting this solution in case anyone else has this problem. 如果有人遇到此问题,请发布此解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM