[Tutor] Extract element of dtype object

Mats Wichmann mats at wichmann.us
Sun Dec 6 14:29:46 EST 2020


On 12/6/20 10:31 AM, Stephen P. Molnar wrote:

> Oh oh. Here is the code:
> 
> import pandas as pd
> 
> df = pd.read_table('test.log')
> df = df.dropna(axis=1)
> print(df.head())
> 
> x = (df.iloc[3])
> print(x)
> 
> from the origional post
> 
> This is an example or a portion of the file:
> 
> mode |   affinity | dist from best mode
>        | (kcal/mol) | rmsd l.b.| rmsd u.b.
> -----+------------+----------+----------
>      1    -8.538750714      0.000      0.000
>           ^^^^^^^^^
>            | |  |  | |  | | | |
>           this is the number I want to extract.

If this is consistent behavior it's very easy.  e.g.:

 >>> line = '    1    -8.538750714      0.000      0.000'
 >>> fields = line.split()
 >>> print(fields)
['1', '-8.538750714', '0.000', '0.000']
 >>> print(fields[1])
-8.538750714
 >>>

but... it seems you aren't reading the line directly, but using Pandas? 
Don't use that myself, but if you process it correctly, it probably 
already knows all of this, and you just need to figure out how it stores 
it...

and if the data lines don't always look the same, then you need to get 
more clever at identifying commonality. i.e. that number you're looking 
for seems to appear in a lot of different ways from your original email...



More information about the Tutor mailing list