Python: Pandas: making a dictionary from dataframe
<p>I am trying to convert a dataframe to dictionary:</p>

<pre><code>xtest_cat = xtest_cat.T.to_dict().values()
</code></pre>

<p>but it gives a warning :</p>

<blockquote>
  <p>Warning: DataFrame columns are not unique, some columns will be omitted python</p>
</blockquote>

<p>I checked the columns names of the dataframe(xtest_cat) : </p>

<pre><code>len(list(xtest_cat.columns.values)) 
len(set(list(xtest_cat.columns.values)))
</code></pre>

<p>they are all unique.
Can anyone help me out ? </p>

<p>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reset_index.html" rel="nofollow"><code>reset_index</code></a> for create <code>unique</code> index:</p>

<pre><code>xtest_cat = pd.DataFrame({'A':[1,2,3],
                          'B':[4,5,6],
                          'C':[7,8,9]})

xtest_cat.index = [0,1,1]
print (xtest_cat)
   A  B  C
0  1  4  7
1  2  5  8
1  3  6  9

print (xtest_cat.index.is_unique)
False

xtest_cat.reset_index(drop=True, inplace=True)
print (xtest_cat)
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

xtest_cat = xtest_cat.T.to_dict().values()
print (xtest_cat)
dict_values([{'B': 4, 'C': 7, 'A': 1}, {'B': 5, 'C': 8, 'A': 2}, {'B': 6, 'C': 9, 'A': 3}])
</code></pre>

<p>You can also omit <code>T</code> and add parameter <code>orient='index'</code>:</p>

<pre><code>xtest_cat = xtest_cat.to_dict(orient='index').values()
print (xtest_cat)
dict_values([{'B': 4, 'C': 7, 'A': 1}, {'B': 5, 'C': 8, 'A': 2}, {'B': 6, 'C': 9, 'A': 3}])
</code></pre>

<p><code>orient='record'</code> is better:</p>

<pre><code>xtest_cat = xtest_cat.to_dict(orient='records')
print (xtest_cat)
[{'B': 4, 'C': 7, 'A': 1}, {'B': 5, 'C': 8, 'A': 2}, {'B': 6, 'C': 9, 'A': 3}]
</code></pre>

python pandas dictionary dataframe multiple-columns