Home > Back-end >  3D Pandas DataFrame Exporting and Importing Problem
3D Pandas DataFrame Exporting and Importing Problem

Time:01-01

I have a data like that;

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

It is perfect formation for me. I can take easily whatever I want and I can see. But unfortunately I have a problem when I'm saving this.

I'm saving like that:

df_3d20.to_csv("BIST100_2020.csv")

and when i try open it again data comes like this:

          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

But i want to import it like first formation i have showed. So, what should i do about importing and exporting 3d pandas dataframes?

CodePudding user response:

Use a 2D array. You can use the Numpy module and use its np.loadtxt() function which returns the data into a 2D array. Or you can use the open() function and pass the given CSV file as an argument to the DictReader(). For example:

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

CodePudding user response:

So instead of exporting and importing a 3 dimensional dataframe, import/export a 2d one, and then you can do what this question does.

  • Related