Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Pandas将词典导出到excel_Python_Excel_Pandas - Fatal编程技术网

Python 如何使用Pandas将词典导出到excel

Python 如何使用Pandas将词典导出到excel,python,excel,pandas,Python,Excel,Pandas,我试图使用Pandas将一些数据从python导出到excel,但没有成功。数据是一个字典,其中键是由4个元素组成的元组 我目前正在使用以下代码: df = pd.DataFrame(data) df.to_excel("*file location*", index=False) 我得到一个导出的2列表,如下所示: 我试图获得一个excel表格,其中键的前3个元素被拆分为各自的列,键的第4个元素(本例中为句点)成为列名,类似于下面的示例: 我曾尝试对上述代码使用不同的

我试图使用Pandas将一些数据从python导出到excel,但没有成功。数据是一个字典,其中键是由4个元素组成的元组

我目前正在使用以下代码:

df = pd.DataFrame(data)
df.to_excel("*file location*", index=False)
我得到一个导出的2列表,如下所示:

我试图获得一个excel表格,其中键的前3个元素被拆分为各自的列,键的第4个元素(本例中为句点)成为列名,类似于下面的示例:


我曾尝试对上述代码使用不同的附加内容,但我对此有点陌生,因此到目前为止没有任何效果

您可以尝试导出到csv

df.to_csv(r'Path where you want to store the exported CSV file\File Name.csv', index = False)
然后可以根据您向我们展示的内容(不可复制),轻松地将其转换为excel文件


嗨,布兰登,欢迎来到苏!您需要意识到这是一个社区驱动的站点,因此您需要尽一切努力使您的问题具有可复制性,这意味着有人可以将您的代码复制到他们的文本编辑器中,并复制您的问题。(你不能复制图片!)阅读并编辑你的问题。如果这个问题结束了,你也可以问一个新问题
df_       = df.set_index(0)  # `0` since your tuples seem to be located at the first column
df_.index = pd.MultiIndex.from_tuples(df_.index)  # We convert your simple index into NDimensional index                 

# `~.unstack` does the job of locating your periods as columns 
df_.unstack(level=-1).droplevel(0, axis=1).to_excel(
    "file location", index=True
)