Jump to content

Python Pandas Package: What is meant by dictionary


zak100

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

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.