简体   繁体   中英

How can I create a single Dataframe out of my python code that generates a dictionary for each iteration of a for loop?

my for loop generates about 80 individual dictionaries:

 {'X: 0.81, 'y': 1.1, 'z': 0.5469} {'X: 0.8, 'y': 1.1, 'z': 0.5464} {'X: 0.79, 'y': 1.1, 'z': 0.5461} {'X: 0.78, 'y': 1.1, 'z': 0.546} But I simply want to send each of those dictionary values as a row in a single data frame with the >dataframe header is equal to the key: xyz 0.81 1.1 0.5469 0.8 1.1 0.5464 0.79 1.1 0.546 0.78 1.1 0.546

Create a list of dictionaries

rows = [
  {'X: 0.81, 'y': 1.1, 'z': 0.5469},
  {'X: 0.8,  'y': 1.1, 'z': 0.5464},
  {'X: 0.79, 'y': 1.1, 'z': 0.5461},
  {'X: 0.78, 'y': 1.1, 'z': 0.546}
]

and then create a DataFrame from them

import pandas as pd
df = pd.DataFrame(rows)

If you care about performance, it is always better to create the list first and then create the DataFrame. You could create the DataFrame first and add rows to it, but that will be slower because it creates a new DataFrame every time and copies everything over.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM