Pandas
keyboard_arrow_down 655 guides
chevron_leftCreating DataFrames Cookbook
Combining multiple Series into a DataFrameCombining multiple Series to form a DataFrameConverting a Series to a DataFrameConverting list of lists into DataFrameConverting list to DataFrameConverting percent string into a numeric for read_csvConverting scikit-learn dataset to Pandas DataFrameConverting string data into a DataFrameCreating a DataFrame from a stringCreating a DataFrame using listsCreating a DataFrame with different type for each columnCreating a DataFrame with empty valuesCreating a DataFrame with missing valuesCreating a DataFrame with random numbersCreating a DataFrame with zerosCreating a MultiIndex DataFrameCreating a Pandas DataFrameCreating a single DataFrame from multiple filesCreating empty DataFrame with only column labelsFilling missing values when using read_csvImporting DatasetImporting tables from PostgreSQL as Pandas DataFramesInitialising a DataFrame using a constantInitialising a DataFrame using a dictionaryInitialising a DataFrame using a list of dictionariesInserting lists into a DataFrame cellKeeping leading zeroes when using read_csvParsing dates when using read_csvPreventing strings from getting parsed as NaN for read_csvReading data from GitHubReading file without headerReading large CSV files in chunksReading n random lines using read_csvReading space-delimited filesReading specific columns from fileReading tab-delimited filesReading the first few lines of a file to create DataFrameReading the last n lines of a fileReading URL using read_csvReading zipped csv file as a DataFrameRemoving Unnamed:0 columnResolving ParserError: Error tokenizing dataSaving DataFrame as zipped csvSkipping rows without skipping header for read_csvSpecifying data type for read_csvTreating missing values as empty strings rather than NaN for read_csv
check_circle
Mark as learned thumb_up
0
thumb_down
0
chat_bubble_outline
0
Comment auto_stories Bi-column layout
settings
Initialising a DataFrame using a dictionary in Pandas
schedule Aug 12, 2023
Last updated local_offer
Tags Python●Pandas
tocTable of Contents
expand_more Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!
Start your free 7-days trial now!
Using a dictionary of arrays
To create a DataFrame using a dictionary of arrays:
df = pd.DataFrame({"A":[3,4], "B":[5,6]})df
A B0 3 51 4 6
Here, the key-value pair of the dictionary is as follows:
key
: column labelvalue
: values of that column
Also, since the data
does not contain any index (row labels), the default integer indices ([0,1]
) are used.
Using a nested dictionary
To create a DataFrame using a nested dictionary:
col_one = {"a":3, "b":4}col_two = {"a":5, "b":6}df = pd.DataFrame({"A":col_one, "B":col_two})df
A Ba 3 5b 4 6
Here, we've specified the index in col_one
and col_two
.
Using a dictionary whose key-value pair represents a row
Consider the following dictionary that holds some data:
data = { "alex": 20, "bob": 30, "cathy": 40}
We want to create a DataFrame whose values are all the key-value pairs of data
. Pandas does not provide a direct solution for this, so we ourselves must extract the keys and values of data
as lists:
arr_name = list(data.keys())arr_age = list(data.values())
We can then use the first approach of initialising a DataFrame using a dictionary of arrays:
df = pd.DataFrame({"name":arr_name, "age":arr_age})df
name age0 alex 201 bob 302 cathy 40
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!