简体   繁体   English

为什么我得到一个没有索引的空 dataFrame

[英]Why am I getting an empty dataFrame with no indexes

I am completely new to all coding so forgive any mistakes in asking the question and please explain like I am 5. I have a file that I've converted to a dataframe but when I try to find a row number corresponding to a specific value it shows up as empty dataframe with a blank for indexes.我对所有编码都是全新的,所以请原谅提出问题时的任何错误,请像我 5 岁一样解释。我有一个文件已转换为 dataframe 但是当我尝试找到对应于特定值的行号时显示为空的 dataframe 索引为空白。 When I use len(pdf.index) it shows that I have 41 rows.当我使用len(pdf.index)时,它显示我有 41 行。 When I try pdf.empty I get an output of false .当我尝试pdf.empty时,我得到一个 output 的false When I use print (pdf.loc[[8]]) it shows row number 8 and the corresponding values.当我使用 print (pdf.loc[[8]])时,它显示第 8 行和相应的值。 But when I try print(pdf.loc[pdf['b'] == 0.078162596]) to find the row number corresponding to that value I get this output.但是当我尝试print(pdf.loc[pdf['b'] == 0.078162596])来查找与该值对应的行号时,我得到了这个 output。

Empty DataFrame
Columns: [a, b, c]
Index: []

This the the code I used to convert the file into a dataframe and the accompanying output.这是我用来将文件转换为 dataframe 和随附的 output 的代码。

pdf = pd.read_fwf('POSCAR_FIRST_LAYER_MANIPULATION copy.txt',header=None,names=['a', 'b', 'c'])
print(pdf)



0   POSCAR file written by  Ovito 3.0.0-dev592        NaN
1                      1.0                 NaN        NaN
2            17.0490970612        0.0000000000   0.000000
3            -0.2785043757       16.9197069925   0.000000
4             9.1773295512        2.1229179060  26.650587
5                       Al                 NaN        NaN
6                       33                 NaN        NaN
7                   Direct                 NaN        NaN
8              0.871711731         0.078162596   0.687544
9              0.760161042         0.924855471   0.689709
10             0.139614120         0.565118968   0.687534
11             0.657146931         0.564061284   0.690766
12             0.566381097         0.062637828   0.709589
13             0.402278483         0.313420027   0.685133
14             0.265235394         0.675979972   0.690942
15             0.172629282         0.802049637   0.692856
16             0.779488027         0.210808992   0.678538
17             0.893642426         0.794784069   0.703213
18             0.278293014         0.198673010   0.728557
19             0.369670391         0.561983645   0.700044
20             0.463950276         0.948694766   0.688976
21             0.265022933         0.467927545   0.680167
22             0.451571882         0.184303001   0.718000
23             0.596131206         0.206098735   0.734343
24             0.125493318         0.328866482   0.689191
25             0.514403701         0.671504021   0.692916
26             0.157217637         0.091310225   0.672789
27             0.301339477         0.911068797   0.704346
28             0.651949406         0.336005211   0.688029
29             0.027625797         0.686297476   0.707778
30             0.519436598         0.489014864   0.687087
31             0.981806457         0.443361163   0.704743
32             0.751180947         0.722405374   0.675096
33             0.418921798         0.795465171   0.685067
34             0.868863404         0.592041790   0.698836
35             0.035270356         0.972233951   0.690336
36             0.358790606         0.075232171   0.693458
37             0.767691195         0.467198342   0.689382
38             0.021950169         0.196752921   0.681510
39             0.894401014         0.301784605   0.696536
40             0.618448853         0.807215154   0.688638

I tried to get rid of the whitespace but that did nothing.我试图摆脱空白,但什么也没做。 How do I get this to not show up as an empty dataframe and get the corresponding row number to the value I enter.我如何让它不显示为空的 dataframe 并将相应的行号获取到我输入的值。 Thank you!谢谢!

You can't compare floating point numbers directly.您不能直接比较浮点数。 Floating point numbers are an approximation.浮点数是一个近似值。 Pandas is showing you the first 9 decimal places, but the number actually has 15 decimal places. Pandas 显示前 9 位小数,但该数字实际上有 15 位小数。 You need to do something like你需要做类似的事情

print(pdf[pdf['b']-0.078162).abs() < 0.00001])

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

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