Reputation: 420
I'm trying to create a dataframe from a dictionary:
dict = {'foo': [1, 2, 3, 4],
'bar': [5, 6, 7, 8]}
and I use the below command to create the dataframe:
df = pd.DataFrame.from_dict(dict, orient='index')
but the output is like:
df:
0 1 2 3
foo 1 2 3 4
bar 4 5 6 7
but I expect the output to be like below with only one column:
df:
'column_name'
foo [1, 2, 3, 4]
bar [4, 5, 6, 7]
Upvotes: 1
Views: 1248
Reputation: 13106
If you're in python3.6+ you can lean on the ordered nature of dictionaries to create a different dictionary of just column_name
being the key:
d = {'foo': [1, 2, 3, 4], 'bar': [5, 6, 7, 8]}
df = pd.DataFrame([{'column_name': v} for v in d.values()], index=d.keys())
df
column_name
foo [1, 2, 3, 4]
bar [5, 6, 7, 8]
Otherwise, I would definitely default to @user3483203's answer
Upvotes: 1
Reputation: 51165
You are passing a dictionary containing "list-like" values. When passed to the DataFrame constructor, pandas
interprets the keys of the dictionaries as Series labels, and the values in each list as new row values per Series.
When you use the from_dict
class method, you are given the option of orientation, which allows you to specify whether or not the keys of the dictionary represent row or column labels, but the "list-like" values of the dictionary will still be interpreted either as new columns or new rows.
Using both of these methods also requires that the lengths of the values are uniform.
pd.DataFrame.from_dict(dct, orient='index')
0 1 2 3
foo 1 2 3 4
bar 5 6 7 8
pd.DataFrame.from_dict(dct, orient='columns')
foo bar
0 1 5
1 2 6
2 3 7
3 4 8
Instead, you are interested in the 1-Dimensional pd.Series
, which will take a dictionary and use each key as a row label, and each value as a row value.
pd.Series(dct)
foo [1, 2, 3, 4]
bar [5, 6, 7, 8]
dtype: object
Per my comment above, if you are interested in a DataFrame, you can use to_frame
, which will maintain the values stored in the Series
.
pd.Series(dct).to_frame('column_name')
column_name
foo [1, 2, 3, 4]
bar [5, 6, 7, 8]
Upvotes: 2
Reputation: 25259
Using the dict to create series and convert to dataframe:
dct = {'foo': [1, 2, 3, 4],
'bar': [5, 6, 7, 8]}
pd.Series(dct).to_frame('column_name')
Out[937]:
column_name
bar [5, 6, 7, 8]
foo [1, 2, 3, 4]
Note: please don't use dict
as the variable name. It's a bad practice.
Upvotes: 1
Reputation: 4827
pd.DataFrame({'col_name': pd.Series(dict)})
Result:
col_name
foo [1, 2, 3, 4]
bar [5, 6, 7, 8]
Upvotes: 1