import pandas as pd
data_dict = {
"key1":1,
"key2":2,
"key3":3
}
data_df = pd.Dataframe.from_dict(data_dict, orient="index")..reset_index().rename(columns={"index": "key", 0: "value"})
"""
data_df =
key value
0 key1 1
1 key2 2
2 key3 3
"""
data_dict = data_df.set_index("key")["value"].to_dict()
"""
data_dict = {
"key1":1,
"key2":2,
"key3":3
}
"""
class Person(object):
def __init__(self, name='', year=0, salary=0):
self.name = name
self.year = year
self.salary = salary
def as_dict(self):
return {'name': self.name, 'year': self.year, 'salary': self.salary}
person1 = Person('john', 2017, 100)
person2 = Person('smith', 2016, 200)
person3 = Person('roger', 2016, 500)
person_list = [person1, person2, person3]
df = pd.DataFrame([x.as_dict() for x in person_list])
print(df)
name salary year
0 john 100 2017
1 smith 200 2016
2 roger 500 2016
|