0

I have a csv that stores a pre-created dataframe so that i can skip the processing and jump back into where i was in the process. The problem is that one of the columns is a dictionary of votes. When stashed in the dataframe it works fine with something like: df.loc[0,'votes']['funny']

But when its loaded from a csv the dictionary is read in as a string. I cant figure out how to convert the string from a single column back into a dictionary without some really hacky code.

I dont particularly want to save each item as its own column but will if thats the prevailing opinion on the way to manage this.

Sample dataframe:

index rest_id   stars   votes
0   Ryq600  2   {'funny': 2, 'useful': 2, 'cool': 2}
1   Ryq600  3   {'funny': 0, 'useful': 3, 'cool': 2}
2   Ryq600  4   {'funny': 1, 'useful': 2, 'cool': 1}
DChaps
  • 346
  • 3
  • 10
  • The post referenced in the close hammer doesn't seem to pertain to what Im trying to get done. But it did provide me with enough nuggets to figure out a solution. First import the csv like normal, then apply `eval` on the single column assigning it back. This will keep it in dictionary form (not break it apart into multiple columns) but make it useable. In my case this would look like `df['votes'] = df['votes'].map(eval)` – DChaps Jul 10 '21 at 00:08

0 Answers0