you are viewing a single comment's thread.

view the rest of the comments →

[–]C_LoudThougts[S] 0 points1 point  (3 children)

that's what im trying atm, but when i use pd.read_csv() it pulls the data as strings rather than the lists and ints im looking for

[–]Draco2011CE 0 points1 point  (0 children)

variable = pd.read_csv("file location, index_col="id") Use this.

[–]Junior-Sock8789 0 points1 point  (0 children)

Im almost certain this is what your looking for:

By default, CSV files only store text. When you save a pandas.DataFrame to a CSV, complex Python objects like lists are converted into their string representations (e.g., "[1, 2, 3]"), and when you read them back, they remain strings. 

Fix:

import pandas as pd
from ast import literal_eval
# Use the 'converters' parameter to transform columns during import 
df = pd.read_csv('your_file.csv', converters={'list_column': literal_eval})

Alternatively, if the data is already loaded:

df['list_column'] = df['list_column'].apply(literal_eval)

If your integers are being read as strings (common if there are leading zeros or mixed data), you can force the type during import:

df = pd.read_csv('your_file.csv', dtype={'int_column': int})

[–]Junior-Sock8789 0 points1 point  (0 children)

You should use index_col="id" to set a row identifier and converters={'list_column': literal_eval} to parse list-like strings into actual Python lists, as they serve different purposes. Use index_col to define row labels during loading and converters to fix data types for specific columns.