Reputation: 129
I am trying to create default dictionary from my pandas DataFrame, but to_dict() method creates unwanted square brackets around values of the columns i want to write. The example code is as follows:
# Create DF
my_df = pd.DataFrame({'numbers': (1, 2, 3, 4, 5), 'letters': ('a', 'b', 'c', 'd', 'e')})
# Create dictionary from the DF
my_dict = my_df.set_index('numbers').T.to_dict('list')
# Create collections dictionary
my_collections_dict = collections.defaultdict(int, my_dict)
results in:
defaultdict(int, {1: ['a'], 2: ['b'], 3: ['c'], 4: ['d'], 5: ['e']})
what i want is:
defaultdict(int, {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'})
how to obtain 'pure' column values?
Upvotes: 1
Views: 2082
Reputation: 10359
You don't need to transpose your frame, you can instead select your column and do:
my_dict = my_df.set_index('numbers')['letters'].to_dict()
if you have multiple columns you want to use in your dictionary, it'll take an extra line but you can use:
my_dict = my_df.set_index('numbers').to_dict(orient='index')
my_dict = {k: list(v.values()) for k, v in my_dict.items()}
Upvotes: 5
Reputation: 1530
It's because you specify to_dict('list')
-> this way entries will be returned as lists (which is why they are shown in []
.
Try using records
instead:
# Create DF
my_df = pd.DataFrame({'numbers': (1, 2, 3, 4, 5), 'letters': ('a', 'b', 'c', 'd', 'e')})
# Create dictionary from the DF
my_dict = my_df.set_index('numbers').T.to_dict('records')
# Create collections dictionary
my_collections_dict = collections.defaultdict(int, my_dict)
Output from the second line:
[{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}]
Upvotes: 1