Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DataFrame index issue
#1
I have a small 2D array which I am trying to save to csv such that when read will be identical to he original array.
I can remove headers, but not the index column.
import csv
import numpy as np
import pandas as pd

Results = np.array([['A','B','C','D'],
                    [2,12,65,'Plc'],
                    [24,41,67,'Plc']])


df = pd.DataFrame(Results)

print('Pre rcovery')
print(df)
print()
print()
#save to csv:
df.to_csv('Results39.csv', header=None, index=None)

#recover from csv:
df2 = pd.read_csv('Results39.csv')

print('Post rcovery') 
print(df2)
The result on running the file is as follows:

Pre rcovery
0 1 2 3
0 A B C D
1 2 12 65 Plc
2 24 41 67 Plc


Post rcovery
A B C D
0 2 12 65 Plc
1 24 41 67 Plc

Whereas I an hoping for

Post rcovery
A B C D
2 12 65 Plc
24 41 67 Plc

I have trawled the net and forums without success - but it has to be a very simple solution ???

Any help greatly appreciated.

Astrikor
Reply
#2
Maybe:
import numpy as np
import pandas as pd
 
results = np.array([['A','B','C','D'],
                    [2,12,65,'Plc'],
                    [24,41,67,'Plc']])
 
df = pd.DataFrame(results)
df.to_csv('results39.csv', header=None, index=None)
df2 = pd.read_csv('results39.csv', header=None)
 
loaded_results = df2.values
print(loaded_results)
print(np.array_equal(loaded_results, results))
Note that since you are keeping mixed types in a numpy array they all end up being strings.
Reply
#3
Thanks Mekira, that works well to return the values and I can cope with them as strings, but I should have asked for the read csv to return the exact original array detail,
i.e. precede the array values with 'Results = np.array'

but it doesn't quite get there:

import numpy as np
import pandas as pd
  
results = np.array([['A','B','C','D'],
                    [2,12,65,'Plc'],
                    [24,41,67,'Plc']])
  
df = pd.DataFrame(results)
df.to_csv('results39.csv', header=None, index=None)
df2 = pd.read_csv('results39.csv', header=None)
  
loaded_results = df2.values
print(loaded_results)
print()
print()
RecoveredResults = 'Results = np.array',loaded_results

print(RecoveredResults)
Run it and the output is as follows:

[['A' 'B' 'C' 'D']
['2' '12' '65' 'Plc']
['24' '41' '67' 'Plc']]


('Results = np.array', array([['A', 'B', 'C', 'D'],
['2', '12', '65', 'Plc'],
['24', '41', '67', 'Plc']], dtype=object))

Any improvement possible??
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  multi index issue of one hot encoder preprocessing aupres 0 1,075 Jun-10-2022, 11:23 AM
Last Post: aupres
  [split] Getting Index Error - list index out of range krishna 2 2,608 Jan-09-2021, 08:29 AM
Last Post: buran
  Interpolating DataFrame method=‘index’ help tlewick1 1 1,843 Oct-22-2020, 12:48 AM
Last Post: scidam
  Issue with dataframe column nsadams87xx 0 1,964 May-29-2020, 02:00 AM
Last Post: nsadams87xx
  Getting Index Error - list index out of range RahulSingh 2 6,146 Feb-03-2020, 07:17 AM
Last Post: RahulSingh
  How to find index of a particular value in a dataframe ankitawadhwa 0 2,381 Jan-21-2020, 09:45 PM
Last Post: ankitawadhwa
  How to add data to the categorical index of dataframe as data arrives? AlekseyPython 1 2,344 Oct-16-2019, 06:26 AM
Last Post: AlekseyPython
  Applying operation to a pandas multi index dataframe subgroup Nuovoq 1 2,652 Sep-04-2019, 10:04 PM
Last Post: Nuovoq
  Panda Dataframe Rounding Issue ab0217 5 7,256 Nov-06-2018, 10:15 PM
Last Post: ichabod801
  How to get first and last row index of each unique names in pandas dataframe SriRajesh 1 4,518 Oct-13-2018, 07:04 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020