pandas函数中pandas.DataFrame.from_dict 直接从字典构建DataFrame 。
DataFrame from_dict()方法用于将Dict转换为DataFrame对象。 此方法接受以下参数。
1)By default the keys of the dict become the DataFrame columns:
默认是将字典的keys作为列
data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']} pd.DataFrame.from_dict(data) col_1 col_2 0 3 a 1 2 b 2 1 c 3 0 d
2) Specify orient='index' to create the DataFrame using dictionary keys as rows: 参数orient为index值时,会将字典的keys作为DataFrame的行
data = {'row_1': [3, 2, 1, 0], 'row_2': ['a', 'b', 'c', 'd']} pd.DataFrame.from_dict(data, orient='index') 0 1 2 3 row_1 3 2 1 0 row_2 a b c d
3) orient为index值时, 可以手动命名列名
pd.DataFrame.from_dict(data, orient='index', columns=['A', 'B', 'C', 'D']) A B C D row_1 3 2 1 0 row_2 a b c d