Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.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 读写具有多行和多列索引的数据帧_Python_Pandas - Fatal编程技术网

Python 读写具有多行和多列索引的数据帧

Python 读写具有多行和多列索引的数据帧,python,pandas,Python,Pandas,我有一个带有多行和多列索引的熊猫数据框架 行索引名称:User\u ID,User\u Name。列索引名称:User\u Type,Sample\u ID 数据帧如下所示 In: user_frame.head(2) Out: Sample_Type TV TV Radio TV Sample_ID 1 2 3 4 User_ID User_Name 1000001 Jim 0.1 0.3 0.5 0.9 1000

我有一个带有多行和多列索引的熊猫数据框架

行索引名称:User\u ID,User\u Name。列索引名称:User\u Type,Sample\u ID

数据帧如下所示

In: user_frame.head(2)

Out:

        Sample_Type TV  TV  Radio TV
        Sample_ID   1   2   3     4
User_ID User_Name
1000001 Jim         0.1 0.3 0.5   0.9
1000001 Julie       0.2 0.9 0.1   0.5
我想把它写到磁盘上

user_frame.to_csv(fi_path,{write_args})
然后用tact中的多行和多列索引将其读回

user_frame_new=pd.read_csv(fi_path,{read_args})
我没有找到同时保留多行和多列索引的正确写参数/读参数

正确的方法是什么

谢谢这里有一个例子:

idx = pd.MultiIndex.from_product([["A", "B"], [1, 2, 3]])
col = pd.MultiIndex.from_product([["X", "Y"], ["aa", "bb"]])
df = pd.DataFrame(np.random.randint(0, 100, size=(6, 4)), index=idx, columns=col)
df.index.names = "name1", "name2"
df.columns.names = "NAME1", "NAME2"

df.to_csv("tmp.csv")
df2 = pd.read_csv("tmp.csv", header=[0, 1], index_col=[0, 1])