Jump to content

Python Pandas Package: What is meant by dictionary

Featured Replies

Hi,

I am following the link at:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html

The link says that

Constructing DataFrame from a dictionary and then it shows the program:

import pandas as pd
d={'col1': [1, 2], 'col2':[3, 4]}
df = pd.DataFrame(data = d)
print (df)


I got the following output:

Quote

 col1  col2
0     1     3
1     2     4


I can't understand the concept of dictionary here. Somebody please guide me with a more meaningful example.

 

Zulfi.

 

 

The python documentation uses the word dictionary to describe variables ithat are key-value pairs. 

The dictionary is defined here. 

d={'col1': [1, 2], 'col2':[3, 4]}

 

Edited by fiveworlds

  • Author

Hi,

Thanks for your response. God blesses you.

 

You mean col1 and col2 are the keys and 1,2 and 3, 4 are the values. Keys are used for searching. But here one key i.e. Col1 or Col2 is corresponding to 2 values. How will we resolve the searching problem?

 

Zulfi.

3 hours ago, zak100 said:

You mean col1 and col2 are the keys and 1,2 and 3, 4 are the values. Keys are used for searching. But here one key i.e. Col1 or Col2 is corresponding to 2 values. How will we resolve the searching problem?

The keys are used to name the columns in the pandas DataFrame. Searching is then using pandas features i guess, example:

import pandas as pd
import sys
d={'col1': [1, 2], 'col2':[3, 4]}
df=pd.DataFrame(data = d)
print(df["col1"].min())
print(df["col1"].mean())
print('find and print the row where col1 has the value 1')
print(df[df["col1"]==1])


Typical output:

1
1.5
   col1  col2
0     1     3

Note: I assume the question is about Pandas since you link to Pandas. Creating DataFrame form a dictionary is just one (convenient) way to create a DataFrame. There are other means to create DataFrame. Searching and filtering the resulting dataframe is explained in the web site you linked to:

https://pandas.pydata.org/pandas-docs/stable/getting_started/intro_tutorials/03_subset_data.html#min-tut-03-subset

The section named "How do I filter specific rows from a DataFrame?" seems to be a good start.

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.