Pandas > Save & Load

Name
Code
Output
# create dataframe
dataframe = pandas.DataFrame(numpy.random.randint(0,100,size=(5, 4)), columns=['C1','C2','C3','C4'])
print ("dataframe")
print (dataframe)
# save dataframe to csv
dataframe.to_csv('dataframe.csv')
# list directory
for entry in os.scandir('.'):
    print(entry.name)
# load dataframe from csv
loaded_dataframe = pandas.read_csv('dataframe.csv',index_col=0)
print ("loaded dataframe")
print (loaded_dataframe)
dataframe
   C1  C2  C3  C4
0  98  82  43  95
1  61  76  19  42
2   3  59  27  40
3  88  13  36  45
4  29  12  43  24
dataframe.csv
loaded dataframe
   C1  C2  C3  C4
0  98  82  43  95
1  61  76  19  42
2   3  59  27  40
3  88  13  36  45
4  29  12  43  24
# requires instaled Python openpyxl module
# create dataframe
dataframe = pandas.DataFrame(numpy.random.randint(0,100,size=(5, 4)), columns=['C1','C2','C3','C4'])
print ("dataframe")
print (dataframe)
# save dataframe to Excel
dataframe.to_excel('dataframe.xlsx',sheet_name='sheet01')
# list directory
for entry in os.scandir('.'):
    print(entry.name)
# load dataframe from csv
loaded_dataframe = pandas.read_excel('dataframe.xlsx',sheet_name='sheet01')
print ("loaded dataframe")
print (loaded_dataframe)
dataframe
   C1  C2  C3  C4
0  81  13  27  17
1  58   0  44  94
2  19   0  49  87
3  12  56  93  53
4  47  92  10  11
dataframe.xlsx
loaded dataframe
   C1  C2  C3  C4
0  81  13  27  17
1  58   0  44  94
2  19   0  49  87
3  12  56  93  53
4  47  92  10  11