Assignment of lists in a matrix

Asked

Viewed 142 times

0

I am creating in a sequence of files with the 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 file name and the line are 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 like above. But not for each file, but to create each element value of the Matrix aij is i of the file and j the value corresponding to the array. Not separated for each file a value line

In order to perform statistical functions with these values.

Thank you very much!

1 answer

0

You can use Dataframe of pandas and do that.

I don’t know an equal tool in , but the does this in a very simplified way. Follow 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
  • I tried, the problem is that it’s doing for each file. I want the result of each file to be all in a single matrix

  • @Murilofretta, In this case, I believe the problem is in the statement! However to verify, I need you to use a [mcve]. Also, your code formatting is not clear. And I advise you to leave the tag [tag:python] in the next issue when you tidy up the code. Python is a relevant tag (just like [tag:pandas] if we continue on this path), it will give more visibility and someone else can help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.