[Tutor] Extract Block of Data from a 2D Array

Stephen P. Molnar s.molnar at sbcglobal.net
Fri Mar 31 11:58:28 EDT 2017


On 03/31/2017 10:50 AM, Peter Otten wrote:
> Stephen P. Molnar wrote:
>
>> I have a block of data extracted from a quantum mechanical calculation:
>>
>> CARTESIAN COORDINATES (A.U.)
>> ----------------------------
>>     NO LB      ZA    FRAG     MASS         X           Y           Z
>>      0 C     6.0000    0    12.011   -3.265636    0.198894    0.090858
>>      1 C     6.0000    0    12.011   -1.307161    1.522212    1.003463
>>      2 C     6.0000    0    12.011    1.213336    0.948208   -0.033373
>>      3 N     7.0000    0    14.007    3.238650    1.041523    1.301322
>>      4 C     6.0000    0    12.011   -5.954489    0.650878    0.803379
>>      5 C     6.0000    0    12.011    5.654476    0.480066    0.013757
>>
>> where the number of lines depends upon the molecule being considered.
>>
>> I want to extract the block of data starting on line 4 and column 4.
>> Unfortunately, the only programming language in which I used to be
>> competent in is Fortran.  I have attempted researching this problem, but
>> have only succeeded in increasing my mental entropy.
>>
>> Help will be much appreciated.  Thanks in advance.
>>
>
> pandas is the swiss army knife of data manipulation in Python -- albeit with
> a non-negligable learning curve. Some examples (from an amateur):
>
> $ cat data.txt
> CARTESIAN COORDINATES (A.U.)
> ----------------------------
>     NO LB      ZA    FRAG     MASS         X           Y           Z
>      0 C     6.0000    0    12.011   -3.265636    0.198894    0.090858
>      1 C     6.0000    0    12.011   -1.307161    1.522212    1.003463
>      2 C     6.0000    0    12.011    1.213336    0.948208   -0.033373
>      3 N     7.0000    0    14.007    3.238650    1.041523    1.301322
>      4 C     6.0000    0    12.011   -5.954489    0.650878    0.803379
>      5 C     6.0000    0    12.011    5.654476    0.480066    0.013757
> $ python3
> Python 3.4.3 (default, Nov 17 2016, 01:08:31)
> [GCC 4.8.4] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import pandas
>>>> table = pandas.read_table("data.txt", skiprows=2, delimiter=" ",
> skipinitialspace=True)
>>>> table
>     NO LB  ZA  FRAG    MASS         X         Y         Z
> 0   0  C   6     0  12.011 -3.265636  0.198894  0.090858
> 1   1  C   6     0  12.011 -1.307161  1.522212  1.003463
> 2   2  C   6     0  12.011  1.213336  0.948208 -0.033373
> 3   3  N   7     0  14.007  3.238650  1.041523  1.301322
> 4   4  C   6     0  12.011 -5.954489  0.650878  0.803379
> 5   5  C   6     0  12.011  5.654476  0.480066  0.013757
>
> [6 rows x 8 columns]
>>>> table[3:]
>     NO LB  ZA  FRAG    MASS         X         Y         Z
> 3   3  N   7     0  14.007  3.238650  1.041523  1.301322
> 4   4  C   6     0  12.011 -5.954489  0.650878  0.803379
> 5   5  C   6     0  12.011  5.654476  0.480066  0.013757
>
> [3 rows x 8 columns]
>>>> table[["X", "Y", "Z"]]
>            X         Y         Z
> 0 -3.265636  0.198894  0.090858
> 1 -1.307161  1.522212  1.003463
> 2  1.213336  0.948208 -0.033373
> 3  3.238650  1.041523  1.301322
> 4 -5.954489  0.650878  0.803379
> 5  5.654476  0.480066  0.013757
>
> [6 rows x 3 columns]
>>>> table.MASS.mean()
> 12.343666666666666
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>

Problem solved!

Many thanks.

-- 
Stephen P. Molnar, Ph.D.		Life is a fuzzy set
www.molecular-modeling.net		Stochastic and multivariate
(614)312-7528 (c)
Skype: smolnar1


More information about the Tutor mailing list