You can create list of tuples and pass to DataFrame
constructor:
df = pd.DataFrame(list(my_keys.items()), columns = ['col1', 'col2'])
Or convert keys and values to separate lists:
df = pd.DataFrame({
'col1': list(my_keys.keys()),
'col2': list(my_keys.values())
})
print(df)
col1 col2
0 a 10
1 b 3
2 c 23
When using the ‘index’ orientation, the column names can be specified manually:,By default the keys of the dict become the DataFrame columns:,Specify orient='index' to create the DataFrame using dictionary keys as rows:,Creates DataFrame object from dictionary by columns or by index allowing dtype specification.
>>> data = {
'col_1': [3, 2, 1, 0],
'col_2': ['a', 'b', 'c', 'd']
} >>>
pd.DataFrame.from_dict(data)
col_1 col_2
0 3 a
1 2 b
2 1 c
3 0 d
>>> data = {
'row_1': [3, 2, 1, 0],
'row_2': ['a', 'b', 'c', 'd']
} >>>
pd.DataFrame.from_dict(data, orient = 'index')
0 1 2 3
row_1 3 2 1 0
row_2 a b c d
>>> pd.DataFrame.from_dict(data, orient = 'index',
...columns = ['A', 'B', 'C', 'D'])
A B C D
row_1 3 2 1 0
row_2 a b c d
>>> data = {
'index': [('a', 'b'), ('a', 'c')],
...'columns': [('x', 1), ('y', 2)],
...'data': [
[1, 3],
[2, 4]
],
...'index_names': ['n1', 'n2'],
...'column_names': ['z1', 'z2']
} >>>
pd.DataFrame.from_dict(data, orient = 'tight')
z1 x y
z2 1 2
n1 n2
a b 1 3
c 2 4
In the above example, the dataframe df is constructed from the dictionary data. And by default, the keys of the dict are treated as column names and their values as respective column values by the pandas dataframe from_dict() function.,By default, it creates a dataframe with the keys of the dictionary as column names and their respective array-like values as the column values. If you want the dictionary keys to be row indexes instead, pass 'index' to the orient parameter (which is 'columns' by default). ,The pandas.DataFrame.from_dict() function is used to create a dataframe from a dict object. The dictionary should be of the form {field: array-like} or {field: dict}. The following is its syntax:,For more on the pandas.DataFrame.from_dict() function refer to its official documentation.
The pandas.DataFrame.from_dict()
function is used to create a dataframe from a dict object. The dictionary should be of the form {field: array-like}
or {field: dict}
. The following is its syntax:
df = pandas.DataFrame.from_dict(data)
1. Pandas dataframe from dict with keys as columns
import pandas as pd # dictionary storing the data data = { "Name": ["Jim", "Dwight", "Angela", "Tobi"], "Age": [26, 28, 27, 32], "Department": ["Sales", "Sales", "Accounting", "Human Resources"] } # dataframe from dict df = pd.DataFrame.from_dict(data) # print the dataframe print(df)
Output:
Name Age Department 0 Jim 26 Sales 1 Dwight 28 Sales 2 Angela 27 Accounting 3 Tobi 32 Human Resources
You can also pass the column names as a list to the columns
parameter when creating a dataframe with orient='index'
import pandas as pd # dictionary storing the data data = { "Jim": [26, "Sales"], "Dwight": [28, "Sales"], "Angela": [27, "Accounting"], "Tobi": [32, "Human Resources"] } # dataframe from dict df = pd.DataFrame.from_dict(data, orient = 'index', columns = ['Age', 'Department']) # print the dataframe print(df)
Last Updated : 16 Aug, 2022
Output:
Geeks For geeks 0 dataframe using list 1 10 20 30
By using the pandas DataFrame constructor you can create a DataFrame from dict (dictionary) object. From dict key-value pair, key represented as column name and values is used for column values in DataFrame.,Python dict (dictionary) which is a key-value pair can be used to create a pandas DataFrame, In real-time, mostly we create a pandas DataFrame by reading a CSV file or from other sources however some times you may need to create it from a dict (dictionary) object.,In my last article, I have explained how easy to create a DataFrame from a list object, similarly, I will explain how easy to create pandas DataFrame from different types of dict (dictionary) objects.,In this article, you have learned to create a DataFrame from the dict by using the DataFames constructor and from_dict() method. Also learned how to add columns and indexes while creating a DataFrame and to this existing one.
By using the pandas DataFrame constructor you can create a DataFrame from dict (dictionary) object. From dict key-value pair, key represented as column name and values is used for column values in DataFrame.
# Dict object courses = { 'Courses': ['Spark', 'PySpark', 'Java', 'PHP'], 'Fee': [20000, 20000, 15000, 10000], 'Duration': ['35days', '35days', '40days', '30days'] } # Create DataFrame from dict df = pd.DataFrame.from_dict(courses) print(df)
You can set custom index to DataFrame.
index = ['r0', 'r1', 'r2', 'r3'] # Create DataFrame with index df = pd.DataFrame.from_dict(courses, index = index) # set index to existing DataFrame df.set_index(index, inplace = True)
# Create for selected columns df = pd.DataFrame(courses, columns = ['Courses', 'Fee']) print(df)
pandas.DataFrame.from_dict()
can be used to create a pandas DataFrame from Dict (Dictionary) object. This method takes parameters data
, orient
, dtype
, columns
and returns a DataFrame. Note that this is a class method which means you can access it from DataFrame class without creating its object.
# Syntax of from_dict() DataFrame.from_dict(data, orient = 'columns', dtype = None, columns = None)
Now pass the dict object to from_dict() method to create. By default it uses orient=columns
.
# Create DataFrame from dict using from_dict() df = pd.DataFrame.from_dict(courses) # set index to existing DataFrame df.set_index(index, inplace = True) print(df)