Assigning lists in an array

0

I am creating in a sequence of files with glob: list (glob.glo ('files'))

Arrays with certain values taken from each cell of a file read as a table

os.chdir ('C: \ * 2018-01-17')

for file in list (glob.glob ('2018011722 * .ascii')):

reader = pd.read_table(file)

df = reader.loc[102:901,:]

df = df.rename(columns={'begin( product )': '0'})

df = df['0'].str.split(',', 799 , expand=True )

df.index = range(800)  


DC = df.iloc[257,221]
if DC == 'nd':
    DC = 0
elif DC != 'nd':
    DC = round(10**(float(DC)/10),1)

FRA = df.iloc[403,667]
if FRA == 'nd':
    FRA = 0
elif FRA != 'nd':
    FRA = round(10**(float(FRA)/10),1)

ITA = df.iloc[426,209]
if ITA == 'nd':
    ITA = 0
elif ITA != 'nd': 
    ITA = round(10**(float(ITA)/10),1)

MON = df.iloc[413,261]
if MON == 'nd':
    MON = 0
elif MON != 'nd': 
    MON = round(10**(float(MON)/10),1)

QUI = df.iloc[335,368]
if QUI == 'nd':
    QUI = 0
elif QUI != 'nd': 
    QUI = round(10**(float(QUI)/10),1)

XAN = df.iloc[365,423]
if XAN == 'nd':
    XAN = 0
elif XAN != 'nd': 
    XAN = round(10**(float(XAN)/10),1)


SMO = df.iloc[346,240]
if SMO == 'nd':
    SMO = 0
elif SMO != 'nd': 
    SMO = round(10**(float(SMO)/10),1)

PAL = df.iloc[409,300]
if PAL == 'nd':
    PAL = 0
elif PAL != 'nd': 
    PAL = round(10**(float(PAL)/10),1)

MAR = df.iloc[340,298]
if MAR == 'nd':
    MAR = 0
elif MAR != 'nd': 
    MAR =round(10**(float(MAR)/10),1)


cidades = np.array(([DC,FRA,ITA,MON,QUI,XAN,SMO,PAL,MAR]))

print(file,cidades)

2018011722000400dBA.pac.ascii [ 0.   0.1  0.2  0.   0.   0.   0.   0.   0. ]

2018011722060400dBA.pac.ascii [ 0.   0.2  0.6  0.   0.   0.   0.   0.   0. ]

2018011722120400dBA.pac.ascii [ 0.   0.2  1.1  0.   0.   0.   0.   0.   0. ]

2018011722180400dBA.pac.ascii [ 0.   0.3  1.9  0.   0.   0.   0.   0.   0. ]

2018011722240400dBA.pac.ascii [ 0.   0.3  1.6  0.   0.   0.   0.   0.   0. ]

2018011722300400dBA.pac.ascii [ 0.   0.2  0.6  0.   0.   0.   0.1  0.   0. ]

2018011722360400dBA.pac.ascii [ 0.   0.   0.2  0.   0.1  0.   0.2  0.   0. ]

2018011722420400dBA.pac.ascii [ 0.   0.   0.1  0.   0.3  0.   0.1  0.   0. ]

2018011722480400dBA.pac.ascii [ 0.   0.   0.1  0.   2.8  0.   0.   0.   0. ]

2018011722540400dBA.pac.ascii [ 0.   0.   0.   0.   5.7  0.   0.   0.   0. ]

Where 2018 * .ascii is the name of the file and the line is the concatenated values.

I'm trying to create an array where the columns are fixed [DC,FRA,ITA,MON,QUI,XAN,SMO,PAL,MAR] And the lines are the files.

I want to create a matrix as above. But not for each file, but create each element value of the Matrix aij i of the file and j the value corresponding to the array. Not separate for each file a line of values

To be able to perform statistical functions with these values.

Thank you!

    
asked by anonymous 19.04.2018 / 00:45

1 answer

0

You can use DataFrame from pandas and do that.

I do not know of an equal tool in , but < pandas does this in a very simplified way. This entry was posted in Uncategorized and tagged pandas, tagged pandas, tagged pandas. Here is an example code:

import pandas as pd
import numpy as np
a=np.random.randn(4,3)
a
array([[ 1.59049005, -0.05743312, -1.03145382],
       [-0.64572951, -0.50805756, -0.19037529],
       [-0.4107107 , -1.38961849,  1.22481743],
       [-0.58295631,  0.31554397, -1.22362196]])

name=['a','b','c','d']
name
['a', 'b', 'c', 'd']

df=pd.DataFrame(a,index=name)
df
          0         1         2
a  1.590490 -0.057433 -1.031454
b -0.645730 -0.508058 -0.190375
c -0.410711 -1.389618  1.224817
d -0.582956  0.315544 -1.223622
    
19.04.2018 / 13:25