提问人:Emrecan Ulu 提问时间:1/1/2023 最后编辑:Julia MeshcheryakovaEmrecan Ulu 更新时间:1/1/2023 访问量:80
3D Pandas DataFrame 导出和导入问题
3D Pandas DataFrame Exporting and Importing Problem
问:
我有这样的数据;
print(df_3d20)
Open Rates Close Rates
Date col_1 col_2 col_3 col_1 col_2 col_3
2020-1-1 0.0 0.0 0.0 0.0 0.0 0.0
2020-1-2 0.0 0.0 0.0 0.0 0.0 0.0
2020-1-3 0.0 0.0 0.0 0.0 0.0 0.0
2020-1-4 0.0 0.0 0.0 0.0 0.0 0.0
这对我来说是完美的阵型。我可以很容易地拿走任何我想要的东西,我可以看到。但不幸的是,我在保存它时遇到了问题。
我是这样保存的:
df_3d20.to_csv("BIST100_2020.csv")
当我再次尝试打开它时,数据是这样的:
Open Rates OpenRates.1 OpenRates.2 Close Rates CloseRates.1 CloseRates.2
Date col_1 col_2 col_3 col_1 col_2 col_3
2020-1-1 0.0 0.0 0.0 0.0 0.0 0.0
2020-1-2 0.0 0.0 0.0 0.0 0.0 0.0
2020-1-3 0.0 0.0 0.0 0.0 0.0 0.0
2020-1-4 0.0 0.0 0.0 0.0 0.0 0.0
但我想像我展示的第一个阵型一样导入它。那么,我应该怎么做才能导入和导出 3d pandas 数据帧呢?
答:
0赞
Pythoneer
1/1/2023
#1
因此,与其导出和导入 3D 数据帧,不如导入/导出 2D 数据帧,然后您就可以执行此问题的作用。
0赞
Toxic.Coder
1/1/2023
#2
使用 2D 数组。您可以使用 Numpy 模块并使用其函数将数据返回到 2D 数组中。或者,您可以使用 open() 函数并将给定的 CSV 文件作为参数传递给 .例如:np.loadtxt()
DictReader()
##### Import the python CSV module
import csv
##### Create a python file object in read mode for the `baby_names.csv` file:
csvfile = open('baby_names.csv', 'r')
##### Loop over a DictReader on the file
for row in csv.DictReader(csvfile):
###### your code
评论