DataFrame creation from dict & index order?

I am using a recent download of Anaconda with Python 3.7.1 & pandas 0.23.4

The pandas doc says:

When the data is a dict, and an index is not passed, the Series index will be ordered by the dict’s insertion order

I instantiate a pandas DataFrame from a dict with no index passed:

newspapers = {'Jim':{'Mon':15,'Tue':17,'Wed':21,'Thu':16,'Fri':19},\
     'Tony':{'Mon':8,'Tue':15,'Wed':11,'Thu':16,'Fri':13}, \
     'Colin':{'Mon':13,'Tue':17,'Wed':19,'Thu':17,'Fri':20} \
    }
newspapers_df = pd.DataFrame(newspapers)

Why does this not show in insertion order, Mon, Tue, Wed, Thu, Fri?:

print(newspapers_df)

outputs:

      Jim  Tony  Colin
Fri   19    13     20
Mon   15     8     13
Thu   16    16     17
Tue   17    15     17
Wed   21    11     19

It seems bug, for me working in python 3.5, pandas 0.24.2 create Series in dictionary comprehension and pass to DataFrame constructor:

newspapers_df = pd.DataFrame({k:pd.Series(v) for k, v in newspapers.items()})

print (newspapers_df)
     Jim  Tony  Colin
Mon   15     8     13
Tue   17    15     17
Wed   21    11     19
Thu   16    16     17
Fri   19    13     20

Possible solutions with your data - DataFrame.reindex or ordered CategoricalIndex:

newspapers_df = pd.DataFrame(newspapers)

L = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
newspapers_df = newspapers_df.reindex(L)

Or:

newspapers_df.index = pd.CategoricalIndex(newspapers_df.index, ordered=True, categories=L)
newspapers_df = newspapers_df.sort_index()
print (newspapers_df)
     Jim  Tony  Colin
Mon   15     8     13
Tue   17    15     17
Wed   21    11     19
Thu   16    16     17
Fri   19    13     20

pandas.DataFrame.to_dict — pandas 1.1.0 documentation, DataFrame.from_dict. Create a DataFrame from a dictionary. df.to_dict('series') {'col1': row1 1 row2 2 Name: col1, dtype: int64, 'col2': row1 0.50 row2 0.75� # Creating Dataframe from Dictionary by Skipping 2nd Item from dict dfObj = pd.DataFrame(studentData, columns=['name', 'city']) As in columns parameter we provided a list with only two column names. So, DataFrame should contain only 2 columns i.e.


The same sorting could be done using numpy.argsort():

days_dict = {'Mon':0, 'Tue':1,'Wed':2,'Thu':3,'Fri':4,'Sat':5,'Sun':6}
df = pd.DataFrame(newspapers).reset_index()
df.iloc[np.argsort(df['index'].map(days_dict)),:]

Python Pandas : How to create DataFrame from dictionary , Create DataFrame from Dictionary using default Constructor. DataFrame constructor accepts a data object that can be ndarray, dictionary etc. i.e.. For dataframe: the key is column. If axis labels are not passed, they will be constructed from the input data based on common sense rules. Note When the data is a dict, and columns is not specified, the DataFrame columns will be ordered by the dict’s insertion order, if you are using Python version >= 3.6 and Pandas >= 0.23.


Looks the pd.Series and pd.DataFrame has different way to handle the case.

I has similar python environment and the result could be replicated in my computer. Also, the Jezrel's pd.Series case could be replicated in my computer.

I check the manual and found both result are folllow the doc.

For series, the key is index When the data is a dict, and an index is not passed, the Series index will be ordered by the dict’s insertion order, if you’re using Python version >= 3.6 and Pandas version >= 0.23.

For dataframe: the key is column. If axis labels are not passed, they will be constructed from the input data based on common sense rules.

Note When the data is a dict, and columns is not specified, the DataFrame columns will be ordered by the dict’s insertion order, if you are using Python version >= 3.6 and Pandas >= 0.23.

That means, if you want to make sure both columns and index sort, you might need pass index or sort them later.

Hope it answer you question.

Convert Python dict into a dataframe, I want to inquire whether there is a way to create a dataframe without creating a dict-of-dicts and then adding each row separately. – anonuser0428 Sep 16 '13� Let’s discuss how to convert Python Dictionary to Pandas Dataframe. We can convert a dictionary to a pandas dataframe by using the pd.DataFrame.from_dict() class-method. Example 1: Passing the key value as a list.


How to Convert a Dictionary to Pandas DataFrame, Next, create the dictionary. For our example, you may use the following code to create the dictionary: my_dict = {'Computer':1500,'Monitor':300,'� to_dict ([orient, into]) Convert the DataFrame to a dictionary. to_excel (excel_writer[, sheet_name, na_rep, …]) Write object to an Excel sheet. to_feather (**kwargs) Write a DataFrame to the binary Feather format. to_gbq (destination_table[, project_id, …]) Write a DataFrame to a Google BigQuery table.


Creating Pandas DataFrames from Lists and Dictionaries, Alternatively you could create your dictionary using python's OrderedDict . sales = OrderedDict([ ('account', [� DataFrame.to_dict(self, orient='dict', into=<class 'dict'>) [source] ¶ Convert the DataFrame to a dictionary. The type of the key-value pairs can be customized with the parameters (see below).


Create pandas dataframe from lists using dictionary, Creating pandas data-frame from lists using dictionary can be achieved in multiple ways. Method #1: Using pandas.DataFrame. With this� You can create a DataFrame from Dictionary by passing a dictionary as the data argument to DataFrame () class. In this tutorial, we shall learn how to create a Pandas DataFrame from Python Dictionary. Syntax – Create DataFrame The syntax to create a DataFrame from dictionary object is shown below.