I wrote my data which was in lists and dicts to a csv file, and when i import the csv file using pd.read_csv('file.csv')
, everything becomes strings. How can i keep or convert it to its original format?
Originally, everything was in a dataframe and then written to a CSV file using df.to_csv(r'./file.csv')
.
JavaScript
x
15
15
1
csv_data['ln2']
2
Out[36]:
3
0 {0: [67, 1], 1: [67, 1], 2: [67, 1], 3: [67, 1...
4
1 {0: [67, 1], 1: [67, 1], 2: [67, 1], 3: [67, 1...
5
2 {0: [67, 1], 1: [67, 1], 2: [67, 1], 3: [67, 1...
6
3 {0: [67, 1], 1: [67, 1], 2: [67, 1], 3: [67, 1...
7
4 {0: [67, 1], 1: [67, 1], 2: [67, 1], 3: [67, 1...
8
9
43244 {0: [71, 1], 1: [73, 1], 2: [79, 1], 3: [80, 1...
10
43245 {0: [71, 1], 1: [73, 1], 2: [79, 1], 3: [80, 1...
11
43246 {0: [71, 1], 1: [73, 1], 2: [79, 1], 3: [80, 1...
12
43247 {0: [71, 1], 1: [73, 1], 2: [79, 1], 3: [80, 1...
13
43248 {0: [71, 1], 1: [73, 1], 2: [79, 1], 3: [80, 1...
14
Name: ln2, Length: 43249, dtype: object
15
all the rows are strings.
Advertisement
Answer
This will read the column as dicts:
JavaScript
1
2
1
df = pd.read_csv('file.csv', converters={'ln2': eval})
2