我用这段代码创建了一个字典:
dat[r["author_name"]] = (r["num_deletions"], r["num_insertions"],
r["num_lines_changed"], r["num_files_changed"], r["author_date"])
我想接下来这些字典并用列创建一只熊猫
author_name | num_deletions | num_insertions | num_lines_changed |num_files changed | author_date
我试过了:
df = pd.DataFrame(list(dat.iteritems()),
columns=['author_name',"num_deletions", "num_insertions", "num_lines_changed",
"num_files_changed", "author_date"])
但它不起作用,因为它只读取字典的键和元组,而不是六列而不是六列。那么如何才能将元组中的五个条目中的每个条目分成它们自己的列
答案 0 :(得分:1)
您需要在相同嵌套级别的键和值:
df = pd.DataFrame([(key,)+val for key, val in dat.items()],
columns=["author_name", "num_deletions",
"num_insertions", "num_lines_changed",
"num_files_changed", "author_date"])
您也可以使用
df = pd.DataFrame.from_dict(dat, orient='index').reset_index()
df.columns = ["author_name", "num_deletions",
"num_insertions", "num_lines_changed",
"num_files_changed", "author_date"]
如果您有大约10,000行或更多行,这似乎会更快一些。
答案 1 :(得分:0)
这应该有用。
import pandas as pd
df = pd.DataFrame(columns=['author_name', 'num_deletions', 'num_insertions', 'num_lines_changed',
'num_files_changed','author_date'])