Save a series of Dataframes in a single python

1

I'm using a function that uses all the values of a column hosted in a file .csv The result of the function is another column with numeric values that is hosted in a previously created .csv file.

To select the column I use iloc[] and in the brackets I add the identifier number of the column that I want to analyze with my function.

To save the result I define a variable that includes the generated dataframe (1 numeric column), I open the file .csv concateno and save:

b = Resultado_de_funcion
a = pd.read_csv('Documento.csv')
c = pd.concat ([a,b],axis=1, ignore_index=True)
c.to_csv('/Users/Desktop/Documento.csv', sep=',', index=False)

Up to here it's great, because I generate my dataFrame that looks like this:

              0                  1                   2                  3
1   0.065674523303752   0.081470105150761   0.091769260076159   0.003526573075701
2   0.105065037258997   0.109204903445907   0.183093275351669   0.003306929861304
3   0.111578685238066   0.12082758028963    0.249087842460461   0.00289069546989
4   0.134153874890082   0.16609003496279    0.331391181462819   0.003440747831614
5   0.146779597122661   0.220918839369483   0.395566346515882   0.003117749159231

So far everything is going very well, just that I need to analyze hundreds and hundreds of columns, so I defined a list that contains the number of columns to analyze and I apply the function doing an iteration:

List=[0,1,3,4,5,612,123,233,222,889]
for j in List:

    dfT = df2.loc[j]

This makes my function apply just as I want to each column, I can see the results in the python shell, but when I see the results in my file .csv the only thing I get is the first one and the Last column.

           0                    889
1   0.065674523303752   0.081470105150761
2   0.105065037258997   0.109204903445907
3   0.111578685238066   0.12082758028963
4   0.134153874890082   0.16609003496279

I think I'm already very close to finishing, but I can not find how to solve this problem, can someone give me new ideas?

Thanks

    
asked by Jonathan Pacheco 09.05.2017 в 00:46
source

1 answer

1

I think it would be best to create a new dataframe made of the columns you want:

lst = [0,1,2,3] #crea una lista de los columnas

stuff = pd.DataFrame() #crea un nuevo dataframe

def keep(x):
    for e in lst:
         stuff[e] = test[e] #empareja la nueva columna con la columna del df
    return stuff

keep(test) #corre la funcion

         0         1           2           3
0   0.065675    0.105065    0.111579    0.134154
1   0.081470    0.109205    0.120828    0.166090
2   0.091769    0.183093    0.249088    0.331391
3   0.003527    0.003307    0.002891    0.003441


stuff.to_csv('stuff.csv') #guarda como csv
    
answered by 21.05.2017 в 04:24