Coder123
Coder123

Reputation: 344

How to write multiple dataframes into one csv file?

I am trying to

Here is my code to pass my two objects through this function to output all results in one csv file. However when i run the code below, it doesnt work.

def get_sf_metadata():
    dd = sf_login()
    metadata=DataFrame(dd.describe()['sobjects'])
    oi=metadata['name']
    sf_obj=['Account', 'Asset']
    for c in oi:
        account_meta = getattr(dd, c).describe()['fields']
        ss=pd.DataFrame(account_meta)
        frames=[ss]
        result = pd.concat(frames)
        result.to_csv('/Users/m/metadata/ff_field_metadata.csv')

get_sf_metadata()

Any ideas or suggestion to fix my script to combine all of my results into one csv file?

Upvotes: 0

Views: 591

Answers (1)

mcskinner
mcskinner

Reputation: 2748

You are overwriting the file on every iteration of the loop.

Try moving your concat and to_csv calls out of the loop. You append each new value to frames during the loop.

def get_sf_metadata():
    dd = sf_login()
    metadata=DataFrame(dd.describe()['sobjects'])
    oi=metadata['name']
    sf_obj=['Account', 'Asset']

    frames = []
    for c in oi:
        account_meta = getattr(dd, c).describe()['fields']
        ss = pd.DataFrame(account_meta)
        frames.append(ss)

    result = pd.concat(frames)
    result.to_csv('/Users/m/metadata/ff_field_metadata.csv')

get_sf_metadata()

Upvotes: 2

Related Questions