카테고리 없음

Create a DataFrame using Pandas

Panda Kim 2022. 11. 10. 01:17

There are 3 animals in a zoo.

Their information is organized in a 2-dimensional table of rows and columns.

We call this DataFrame.

Let's make it using Pandas library.


Import the library.

import pandas as pd

 

The headers(Name, Food, Age) are column names.

You are going to make each column one by one, and then combine them together.

That's our plan.


Make a list with Panda, Rabbit, Lion.

All the elements should be string.

The 'Name' will be matched to the list with a colon(:).

'Name' : ['Panda', 'Rabbit', 'Lion']

Do the same thing with 'Food'.

'Food' : ['Bamboo', 'Carrot',  'Meat']

Also with 'Age'.

In this case, The elements can be string or numeric(int/float).

The point is all the elements should be in the same data type.

'Age' : [5, 2, 7]

Make those 3 lists into a dictionary.

Separate the lines with commas, and put them in braces.

Assign the dictionary to a variable.

dict_zoo = {'Name' : ['Panda', 'Rabbit', 'Lion'],
	    'Food' : ['Bamboo', 'Carrot', 'Meat'],
	    'Age' : [5, 2, 7]}

You're all set.

pd.DataFrame() is the most basic function in pandas.

Let's put the dictionary in the function.

pd.DataFrame(dict_zoo)

Ta-da.


Code Summary

import pandas as pd

dict_zoo = {'Name' : ['Panda', 'Rabbit', 'Lion'],
	    'Food' : ['Carrot', 'Bamboo', 'Meat'],
	    'Age' : [5, 2, 7]}
       
pd.DataFrame(dict_zoo)

You can make the same result through other different ways.

For example, the input of pd.DataFrame() should not necessarily be a dictionary.

They can be determined with the function's arguments.

 

data = [['Panda', 'Rabbit', 'Lion'],
        ['Carrot', 'Bamboo', 'Meat'],
        [5, 2, 7]]

headers = ['Name', 'Food', 'Age']

pd.DataFrame(data, columns = headers)

 

You can see the {braces} are replaced with [brackets],

and the headers are also organized into a list, and put into the function argument.