TAC
TAC

Reputation: 25

append row to dataframe

I import a couple of txt files and proceed them through different functions. Afterwards I get a bunch of values (here referred as A B C) which I want to export all in one for every txt file. But the append(row) does not work. It does not add a new row to the results (which is a dataframe). I also tried it if I convert row to a dataframe before adding. That did not work either. If I let the code run results remains empty.

import numpy as np
import pandas as pd
import operator
import sys
sys.path.append("../../src/")


fids = [file for file in os.listdir(path_data)]
d = dict()


result = {'maximal Depth': [], 'gradient at maximal Depth': [], 'minimal Depth': []
result= pd.DataFrame(result)
for val in d:
txt_fid=d[val]


df = pd.DataFrame(txt_fid)    
a = max(df[‘A'].tolist())
c = min(df[‘C'].tolist())
b= df[‘B'].mean()
row = {‘value a’: [a], ‘value B’: [b], ‘value C': [c]}
result.append(row,ignore_index=True)
result.to_csv('C:///U....2.csv',index=False)

that did not work either: row = pd.DataFrame.from_dict(row)

Upvotes: 1

Views: 103

Answers (1)

vlemaistre
vlemaistre

Reputation: 3331

result.append(row) does append row to result but does not save it in result. You should write:

result = result.append(row,ignore_index=True)

Upvotes: 1

Related Questions