Specify a list of your desired ordering to the columns
attribute, and DataFrame
will reorder the columns when creating the DataFrame.
pd.DataFrame(results, columns = ['Name', 'Age'])
Name Age
0 alice 3
Or, DataFrame.from_records
does this as well.
pd.DataFrame.from_records(results, columns = ['Name', 'Age'])
Name Age
0 alice 3
Another idea, fix the columns if the ordering is incorrect.
if df.columns.get_loc('Age') < df.columns.get_loc('Name'):
df.insert(df.columns.get_loc('Age'), 'Name', df.pop('Name'))
IIUC:
df = pd.DataFrame(results, columns = list(row.keys()))
#from collections
import OrderedDict: alternative
#columns = [i
for i in OrderedDict.fromkeys(row.keys())
]
print(df)
Name Age 0 alice 3
According to the docs, as of pandas 0.23 and python 3.6, when constructing from dicts, the order will be maintained. You could simply use an OrderedDict
.
import pandas as pd from collections import OrderedDict results = [] row = OrderedDict() row['Name'] = "alice" row['Age'] = 3 results.append(row) #..reset row, fill in values like above, and append more rows.. df = pd.DataFrame(results) print(df.head())
Name Age 0 alice 3
Last Updated : 16 Aug, 2022
Output:
Geeks For geeks 0 dataframe using list 1 10 20 30
As all the dictionaries have similar keys, so the keys became the column names. Then for each key all the values associated with that key in all the dictionaries became the column values. Also, all the items from the index list were used as indexes in the dataframe.,As all the dictionaries in the list had similar keys, so the keys became the column names. Then for each key, values of that key in all the dictionaries became the column values. As we didn’t provide any index argument, so dataframe has default indexes i.e. 0 to N-1.,Create Dataframe from list of dictionaries with custom indexes,Here we passed a list of dictionaries as the first argument, but in columns argument we provided the names of all keys except one. Therefore Dataframe didn’t have any column for that particular key.
Suppose we have a list of python dictionaries i.e.
list_of_dict = [{
'Name': 'Shaun',
'Age': 35,
'Marks': 91
},
{
'Name': 'Ritika',
'Age': 31,
'Marks': 87
},
{
'Name': 'Smriti',
'Age': 33,
'Marks': 78
},
{
'Name': 'Jacob',
'Age': 23,
'Marks': 93
},
]
Name Age Marks
a Shaun 35 91
b Ritika 31 87
c Smriti 33 78
d Jacob 23 93
We can achieve this using Dataframe constructor i.e.
pandas.DataFrame(data = None, index = None, columns = None, dtype = None, copy = False)
Output:
Name Age Marks
0 Shaun 35 91
1 Ritika 31 87
2 Smriti 33 78
3 Jacob 23 93
We can pass a list of indexes along with the list of dictionaries in the Dataframe constructor,
import pandas as pd list_of_dict = [{ 'Name': 'Shaun', 'Age': 35, 'Marks': 91 }, { 'Name': 'Ritika', 'Age': 31, 'Marks': 87 }, { 'Name': 'Smriti', 'Age': 33, 'Marks': 78 }, { 'Name': 'Jacob', 'Age': 23, 'Marks': 93 }, ] # Create Dataframe from list of dictionaries and # pass another list as index df = pd.DataFrame(list_of_dict, index = ['a', 'b', 'c', 'd']) print(df)
I have a method that creates a list, results, and then appends to it a dict row which has keys (column names in my eventual dataframe) and values (row values in my eventual dataframe). Then I append the row and convert the results collection to a df at the end. For example:,I’ve written about constructing DataFrames from records in this post: Convert list of dictionaries to a pandas DataFrame,Specify a list of your desired ordering to the columns attribute, and DataFrame will reorder the columns when creating the DataFrame.,The issue I have is that the column names are alphbetized so the df looks like this:
results = []
row = {}
row['Name'] = "alice"
row['Age'] = 3
results.append(row)..reset row, fill in values like above, and append more rows..
df = pd.DataFrame(results)
df.head() | Age | Name | | 3 | alice |
cols = df.columns cols = cols[: 1] + cols[0: 1]
pd.DataFrame(results, columns = ['Name', 'Age'])
Name Age
0 alice 3
pd.DataFrame.from_records(results, columns = ['Name', 'Age'])
Name Age
0 alice 3
if df.columns.get_loc('Age') < df.columns.get_loc('Name'):
df.insert(df.columns.get_loc('Age'), 'Name', df.pop('Name'))
1. Create a DataFrame from List of Dict,Pandas – Create DataFrame From Dict (Dictionary),Pandas – Create DataFrame,Pandas Create Empty DataFrame
# Below are quick example # Create a list of dictionary objects technologies = [{ 'Courses': 'Spark', 'Duration': '30days', 'Discount': 1000 } { 'Courses': 'python', 'Fee': 25000, 'Courses_Fee': 'Spark' }, { 'Fee': 30000, 'Duration': '35days', 'Duration_Discount': '10days' } ] # Convert a List of dictionaries using from_records() method. df = pd.DataFrame.from_records(technologies) # Set Custom index by using index parameter. df = pd.DataFrame.from_records(technologies, index = ['1', '2', '3']) # Convert a List of Dictionaries by from_dict method. df = pd.DataFrame.from_dict(data) # Dictionary orientations of column. df = pd.DataFrame.from_dict(technologies, orient = 'columns') # Convert a list of dictionaries using json_normalize(). df = pd.json_normalize(technologies)
If you have a list of dictionaries (dict), it is easy to create a DataFrame by using the DataFrame constructor. For more examples refer to how to create a pandas DataFrame with examples.
# Create a DataFrame from list of Dictionaries with default indexes. import pandas as pd # List of dict object technologies = [{ 'Courses': 'Spark', 'Duration': '30days', 'Discount': 1000 } { 'Courses': 'python', 'Fee': 25000, 'Courses_Fee': 'Spark' }, { 'Fee': 30000, 'Duration': '35days', 'Duration_Discount': '10days' } ] # Create DataFrame from list of dic object df = pd.DataFrame(technologies) print(df)
Yields below output. Note that when a key is not found for some dicts and it exists on other dicts, it creates a DataFrame with NaN
for non-existing keys. In case you would like to change the NaN values refer to How to replace NaN/None values with empty String.
Courses Duration Discount Fee Courses_Fee Duration_Discount 0 Spark 30 days 1000.0 NaN NaN NaN 1 python NaN NaN 25000.0 Spark NaN 2 NaN 35 days NaN 30000.0 NaN 10 days
In this section, let’s see how to set custom index by using index
parameter. If you already have a DataFrame, you can set the Index to the DataFrame by using df.index
.
# Set Custom index by using index parameter. df = pd.DataFrame.from_records(technologies, index = ['1', '2', '3']) print(df)
Courses Duration Discount Fee Courses_Fee Duration_Discount 1 Spark 30 days 1000.0 NaN NaN NaN 2 python NaN NaN 25000.0 Spark NaN 3 NaN 35 days NaN 30000.0 NaN 10 days
The code example creates a DataFrame and then converts it to a list of dictionaries. The result variable contains the list of dictionaries.,You can pass 'records' as a parameter to the to_dict() method of the pandas DataFrame to convert a DataFrame to the list of dictionaries.,The function DataFrame.T.to_dict().values() will output the same result as to_dict('records') but you need to use the list() function of python to convert the output to a list of dictionaries. Check the below code example for reference.,If you have a DataFrame and you want to convert it into a list of dictionaries, you can use the DataFrame.to_dict('records') function. This function will take your DataFrame and return a list of dictionaries, where each dictionary represents one row of the DataFrame.
import pandas as pd # create dataframe and add column and rows to it df_fruits = pd.DataFrame() df_fruits["fruit_name"] = ["Orange", "Banana", "Apple", "Grapes"] df_fruits["color"] = ["orange", "yellow", "red", "green"] df_fruits["price"] = [50, 100, 150, 70] # convert DataFrame to list of dictionaries result = df_fruits.to_dict('records') print(result)
Output
[{
'fruit_name': 'Orange',
'color': 'orange',
'price': 50
},
{
'fruit_name': 'Banana',
'color': 'yellow',
'price': 100
},
{
'fruit_name': 'Apple',
'color': 'red',
'price': 150
},
{
'fruit_name': 'Grapes',
'color': 'green',
'price': 70
}
]
Our DataFrame will look like this:
╒════╤══════════════╤═════════╤═════════╕││
fruit_name│ color│ price│╞════╪══════════════╪═════════╪═════════╡│ 0│ Orange│ orange│ 50│├────┼──────────────┼─────────┼─────────┤│ 1│ Banana│ yellow│ 100│├────┼──────────────┼─────────┼─────────┤│ 2│ Apple│ red│ 150│├────┼──────────────┼─────────┼─────────┤│ 3│ Grapes│ green│ 70│╘════╧══════════════╧═════════╧═════════╛
Syntax
DataFrame.to_dict('records')
DataFrame output
╒════╤══════════════╤════════════════╤═════════╕││ product_id│ product_name│ price│╞════╪══════════════╪════════════════╪═════════╡│ 0│ 1000│ P01│ 330│├────┼──────────────┼────────────────┼─────────┤│ 1│ 1001│ P02│ 510│├────┼──────────────┼────────────────┼─────────┤│ 2│ 1002│ P03│ 23│├────┼──────────────┼────────────────┼─────────┤│ 3│ 1003│ P04│ 590│├────┼──────────────┼────────────────┼─────────┤│ 4│ 1004│ P05│ 620.4│╘════╧══════════════╧════════════════╧═════════╛
After converting to a list of dictionaries:
[{
'product_id': 1000,
'product_name': 'P01',
'price': 330.0
},
{
'product_id': 1001,
'product_name': 'P02',
'price': 510.0
},
{
'product_id': 1002,
'product_name': 'P03',
'price': 23.0
},
{
'product_id': 1003,
'product_name': 'P04',
'price': 590.0
},
{
'product_id': 1004,
'product_name': 'P05',
'price': 620.4
}
]