I have a df as shown below
Params Value
teachers 49
students 289
R 3.7
holidays 165
OS 18
Em_from 2020-02-29T20:00:00.000Z
Em_to 2020-03-20T20:00:00.000Z
Em_F 3
Em_C 2
sC_from 2020-03-31T20:00:00.000Z
sC_to 2020-05-29T20:00:00.000Z
sC_F 25
sC_C 31
From the above df I would like to convert that as a dictionary of dictionary as shown below.
dict:
{'teachers': 49,
'students': 289,
'R': 3.7,
'holidays': 165,
'OS':18,
'Em': {'from': '2020-02-29T20:00:00.000Z', 'to': '2020-03-20T20:00:00.000Z',
'F': 3, 'C': 2},
'sC': {'from': '2020-03-31T20:00:00.000Z', 'to': '2020-05-29T20:00:00.000Z',
'F': 25, 'C': 31}}
Use:
s = df['Params'].str.split('_')
m = s.str.len().eq(1)
d1 = df[m].set_index('Params')['Value'].to_dict()
d2 = df[~m].assign(Params=s.str[-1]).agg(tuple, axis=1)\
.groupby(s.str[0]).agg(lambda s: dict(s.tolist())).to_dict()
dct = {**d1, **d2}
Result:
{'Em': {'C': '2',
'F': '3',
'from': '2020-02-29T20:00:00.000Z',
'to': '2020-03-20T20:00:00.000Z'},
'OS': '18',
'R': '3.7',
'holidays': '165',
'sC': {'C': '31',
'F': '25',
'from': '2020-03-31T20:00:00.000Z',
'to': '2020-05-29T20:00:00.000Z'},
'students': '289',
'teachers': '49'}
Please always try to provide the data in a reproducible way, more people will be able to attempt the question
Dataset
Params = ['teachers','students','R','holidays','OS','Em_from','Em_to','Em_F','Em_C','sC_from','sC_to','sC_F','sC_C']
Value = ['49','289','3.7','165','18','2020-02-29T20:00:00.000Z','2020-03-20T20:00:00.000Z','3','2','2020-03-31T20:00:00.000Z','2020-05-29T20:00:00.000Z','25','31']
df = pd.DataFrame(zip(Params,Value),columns=["col1","col2"])
you can do something like
d = {}
for lst in df.values:
for k,v in zip(lst[0:],lst[1:]):
if any(name in k for name in ('Em_from', 'sC_from')):d[k.split('_')[0]] = {k.split('_')[1]:v}
elif any(name in k for name in ('Em_to', 'Em_F','Em_C','sC_to','sC_F','sC_C')):d[k.split('_')[0]][k.split('_')[1]] = v
else:d[k] = v
Output
{'teachers': '49',
'students': '289',
'R': '3.7',
'holidays': '165',
'OS': '18',
'Em': {'from': '2020-02-29T20:00:00.000Z',
'to': '2020-03-20T20:00:00.000Z',
'F': '3',
'C': '2'},
'sC': {'from': '2020-03-31T20:00:00.000Z',
'to': '2020-05-29T20:00:00.000Z',
'F': '25',
'C': '31'}}
panda's dataframes have a to_json method (see docs)
There are multiple examples there, but the general flow goes like this, let's say you have a dataframe called df:
import json
import pandas as pd
parsed = df.to_json()
df_json = json.loads(json_df)
Read the docs to see more examples and different parameters you may have to fiddle with.
I have a DataFrame with four columns. I want to convert this DataFrame to a python dictionary. I want the elements of first column be keys and the elements of other columns in same row be values.
DataFrame:
ID A B C
0 p 1 3 2
1 q 4 3 2
2 r 4 0 9
Output should be like this:
Dictionary:
{'p': [1,3,2], 'q': [4,3,2], 'r': [4,0,9]}
The to_dict() method sets the column names as dictionary keys so you'll need to reshape your DataFrame slightly. Setting the 'ID' column as the index and then transposing the DataFrame is one way to achieve this.
to_dict() also accepts an 'orient' argument which you'll need in order to output a list of values for each column. Otherwise, a dictionary of the form {index: value} will be returned for each column.
These steps can be done with the following line:
>>> df.set_index('ID').T.to_dict('list')
{'p': [1, 3, 2], 'q': [4, 3, 2], 'r': [4, 0, 9]}
In case a different dictionary format is needed, here are examples of the possible orient arguments. Consider the following simple DataFrame:
>>> df = pd.DataFrame({'a': ['red', 'yellow', 'blue'], 'b': [0.5, 0.25, 0.125]})
>>> df
a b
0 red 0.500
1 yellow 0.250
2 blue 0.125
Then the options are as follows.
dict - the default: column names are keys, values are dictionaries of index:data pairs
>>> df.to_dict('dict')
{'a': {0: 'red', 1: 'yellow', 2: 'blue'},
'b': {0: 0.5, 1: 0.25, 2: 0.125}}
list - keys are column names, values are lists of column data
>>> df.to_dict('list')
{'a': ['red', 'yellow', 'blue'],
'b': [0.5, 0.25, 0.125]}
series - like 'list', but values are Series
>>> df.to_dict('series')
{'a': 0 red
1 yellow
2 blue
Name: a, dtype: object,
'b': 0 0.500
1 0.250
2 0.125
Name: b, dtype: float64}
split - splits columns/data/index as keys with values being column names, data values by row and index labels respectively
>>> df.to_dict('split')
{'columns': ['a', 'b'],
'data': [['red', 0.5], ['yellow', 0.25], ['blue', 0.125]],
'index': [0, 1, 2]}
records - each row becomes a dictionary where key is column name and value is the data in the cell
>>> df.to_dict('records')
[{'a': 'red', 'b': 0.5},
{'a': 'yellow', 'b': 0.25},
{'a': 'blue', 'b': 0.125}]
index - like 'records', but a dictionary of dictionaries with keys as index labels (rather than a list)
>>> df.to_dict('index')
{0: {'a': 'red', 'b': 0.5},
1: {'a': 'yellow', 'b': 0.25},
2: {'a': 'blue', 'b': 0.125}}
Should a dictionary like:
{'red': '0.500', 'yellow': '0.250', 'blue': '0.125'}
be required out of a dataframe like:
a b
0 red 0.500
1 yellow 0.250
2 blue 0.125
simplest way would be to do:
dict(df.values)
working snippet below:
import pandas as pd
df = pd.DataFrame({'a': ['red', 'yellow', 'blue'], 'b': [0.5, 0.25, 0.125]})
dict(df.values)
Follow these steps:
Suppose your dataframe is as follows:
>>> df
A B C ID
0 1 3 2 p
1 4 3 2 q
2 4 0 9 r
1. Use set_index to set ID columns as the dataframe index.
df.set_index("ID", drop=True, inplace=True)
2. Use the orient=index parameter to have the index as dictionary keys.
dictionary = df.to_dict(orient="index")
The results will be as follows:
>>> dictionary
{'q': {'A': 4, 'B': 3, 'D': 2}, 'p': {'A': 1, 'B': 3, 'D': 2}, 'r': {'A': 4, 'B': 0, 'D': 9}}
3. If you need to have each sample as a list run the following code. Determine the column order
column_order= ["A", "B", "C"] # Determine your preferred order of columns
d = {} # Initialize the new dictionary as an empty dictionary
for k in dictionary:
d[k] = [dictionary[k][column_name] for column_name in column_order]
Try to use Zip
df = pd.read_csv("file")
d= dict([(i,[a,b,c ]) for i, a,b,c in zip(df.ID, df.A,df.B,df.C)])
print d
Output:
{'p': [1, 3, 2], 'q': [4, 3, 2], 'r': [4, 0, 9]}
If you don't mind the dictionary values being tuples, you can use itertuples:
>>> {x[0]: x[1:] for x in df.itertuples(index=False)}
{'p': (1, 3, 2), 'q': (4, 3, 2), 'r': (4, 0, 9)}
For my use (node names with xy positions) I found #user4179775's answer to the most helpful / intuitive:
import pandas as pd
df = pd.read_csv('glycolysis_nodes_xy.tsv', sep='\t')
df.head()
nodes x y
0 c00033 146 958
1 c00031 601 195
...
xy_dict_list=dict([(i,[a,b]) for i, a,b in zip(df.nodes, df.x,df.y)])
xy_dict_list
{'c00022': [483, 868],
'c00024': [146, 868],
... }
xy_dict_tuples=dict([(i,(a,b)) for i, a,b in zip(df.nodes, df.x,df.y)])
xy_dict_tuples
{'c00022': (483, 868),
'c00024': (146, 868),
... }
Addendum
I later returned to this issue, for other, but related, work. Here is an approach that more closely mirrors the [excellent] accepted answer.
node_df = pd.read_csv('node_prop-glycolysis_tca-from_pg.tsv', sep='\t')
node_df.head()
node kegg_id kegg_cid name wt vis
0 22 22 c00022 pyruvate 1 1
1 24 24 c00024 acetyl-CoA 1 1
...
Convert Pandas dataframe to a [list], {dict}, {dict of {dict}}, ...
Per accepted answer:
node_df.set_index('kegg_cid').T.to_dict('list')
{'c00022': [22, 22, 'pyruvate', 1, 1],
'c00024': [24, 24, 'acetyl-CoA', 1, 1],
... }
node_df.set_index('kegg_cid').T.to_dict('dict')
{'c00022': {'kegg_id': 22, 'name': 'pyruvate', 'node': 22, 'vis': 1, 'wt': 1},
'c00024': {'kegg_id': 24, 'name': 'acetyl-CoA', 'node': 24, 'vis': 1, 'wt': 1},
... }
In my case, I wanted to do the same thing but with selected columns from the Pandas dataframe, so I needed to slice the columns. There are two approaches.
Directly:
(see: Convert pandas to dictionary defining the columns used fo the key values)
node_df.set_index('kegg_cid')[['name', 'wt', 'vis']].T.to_dict('dict')
{'c00022': {'name': 'pyruvate', 'vis': 1, 'wt': 1},
'c00024': {'name': 'acetyl-CoA', 'vis': 1, 'wt': 1},
... }
"Indirectly:" first, slice the desired columns/data from the Pandas dataframe (again, two approaches),
node_df_sliced = node_df[['kegg_cid', 'name', 'wt', 'vis']]
or
node_df_sliced2 = node_df.loc[:, ['kegg_cid', 'name', 'wt', 'vis']]
that can then can be used to create a dictionary of dictionaries
node_df_sliced.set_index('kegg_cid').T.to_dict('dict')
{'c00022': {'name': 'pyruvate', 'vis': 1, 'wt': 1},
'c00024': {'name': 'acetyl-CoA', 'vis': 1, 'wt': 1},
... }
Most of the answers do not deal with the situation where ID can exist multiple times in the dataframe. In case ID can be duplicated in the Dataframe df you want to use a list to store the values (a.k.a a list of lists), grouped by ID:
{k: [g['A'].tolist(), g['B'].tolist(), g['C'].tolist()] for k,g in df.groupby('ID')}
Dictionary comprehension & iterrows() method could also be used to get the desired output.
result = {row.ID: [row.A, row.B, row.C] for (index, row) in df.iterrows()}
df = pd.DataFrame([['p',1,3,2], ['q',4,3,2], ['r',4,0,9]], columns=['ID','A','B','C'])
my_dict = {k:list(v) for k,v in zip(df['ID'], df.drop(columns='ID').values)}
print(my_dict)
with output
{'p': [1, 3, 2], 'q': [4, 3, 2], 'r': [4, 0, 9]}
With this method, columns of dataframe will be the keys and series of dataframe will be the values.`
data_dict = dict()
for col in dataframe.columns:
data_dict[col] = dataframe[col].values.tolist()
DataFrame.to_dict() converts DataFrame to dictionary.
Example
>>> df = pd.DataFrame(
{'col1': [1, 2], 'col2': [0.5, 0.75]}, index=['a', 'b'])
>>> df
col1 col2
a 1 0.1
b 2 0.2
>>> df.to_dict()
{'col1': {'a': 1, 'b': 2}, 'col2': {'a': 0.5, 'b': 0.75}}
See this Documentation for details
I have a data like below,
resultFromCalculation = [{'value40': {'A': 3.1, 'B': 5.62, 'C': 5.99, 'D': 5.06, 'E': 5.09}},
{'value50': {'A': 2.95, 'B': 5.21, 'C': 5.41, 'D': 4.64, 'E': 4.5}},
{'value60': {'A': 2.35, 'B': 4.8, 'C': 4.83, 'D': 4.08, 'E': 3.62}},
{'value70': {'A': 2.95, 'B': 5.21, 'C': 5.41, 'D': 4.64, 'E': 4.5}}]
I want to find average for A to E values for each list. Like,
avgValues = [{'value40':4.97},{'value50':4.41},{'value60':3.99},{'value70':3.99}]
From the above OP I need to find out which one is first least value than others.
FinalResultIs = value60
Using Pandas:
>>> pd.concat([pd.DataFrame(d) for d in resultFromCalculation], axis=1).mean()
value40 4.972
value50 4.542
value60 3.936
value70 4.542
dtype: float64
>>> pd.concat([pd.DataFrame(d) for d in resultFromCalculation], axis=1).mean().argmin()
'value60'
Using simple list comprehension, you can use
avgValues = [{list(d.keys())[0]: sum(list(d.values())[0].values()) / len(list(d.values())[0].values())} for d in resultFromCalculation]
>>> avgValues
[{'value40': 4.9719999999999995},
{'value50': 4.542},
{'value60': 3.936000000000001},
{'value70': 4.542}]
To find the minimum:
>>> min(avgValues, key=lambda e: list(e.values())[0])
{'value60': 3.936000000000001}
Use:
L = [pd.DataFrame(x).mean().to_dict() for x in resultFromCaluclation]
print (L)
[{'value40': 4.9719999999999995}, {'value50': 4.542}, {'value60': 3.936000000000001}, {'value70': 4.542}]
If I have one integer and multiply it by each integer in a container (tuple) and add them together -- similar to a dot product -- I get the right answer. When I convert them to floats, I get a TypeError:
TypeError: can't multiply sequence by non-int of type 'float'
sig = {'a': 1.0, 'b': 2.0, 'c': 3.0}
exp = {'a': (1.0,2.0,3.0), 'b': (1.0,2.0,3.0), 'c': (1.0,2.0,3.0)}
man_dot = {'a': 1*1+1*2+1*3, 'b': 2*1+2*2+2*3, 'c': 3*1+3*2+3*3}
weighted_dict = {}
for s in sig:
print("this is s:\n{}".format(s))
for e in exp:
print("this is e:\n{}".format(e))
weighted_dict[s] = sum(sig[s] * exp[e])
# weighted_dict should be equivalent to man_dot
# weighted_dict should be {'a': 6, 'c': 18, 'b': 12}
This script must handle operation with floats, so how can I modify it to do so? Why does this happen? Is there a better of of doing this with some math-oriented library?
Your problem is that you are trying to multiply (1.0, 2.0, 3.0) by 1.0, which gives the aforementioned error. Try the following:
sig = {'a': 1.0, 'b': 2.0, 'c': 3.0}
exp = {'a': (1.0,2.0,3.0), 'b': (1.0,2.0,3.0), 'c': (1.0,2.0,3.0)}
man_dot = {'a': 1*1+1*2+1*3, 'b': 2*1+2*2+2*3, 'c': 3*1+3*2+3*3}
weighted_dict = {}
for s in sig:
print("this is s:\n{}".format(s))
for e in exp:
print("this is e:\n{}".format(e))
weighted_dict[s] = sum([sig[s] * item for item in exp[e]])
>>> weighted_dict
{'c': 18.0, 'a': 6.0, 'b': 12.0}
>>>