My Coding > Programming language > Python > Python libraries and packages > Python Panda

Python Panda (Page: 2)

Go to Page:

  1. Panda Series;
  2. Pandas DataFrame: Creation;
  3. Pandas: Create test DataFrame;
  4. Pandas DataFrame: Add/Remove;
  5. Pandas DataFrame: Export/Import;
  6. Panda search and select;
  7. Pandas Cheat Sheet;
  8. Pandas: MultiIndex DataFrame;

Pandas DataFrame is a two dimensional array (table structure), where each column is made of Pandas Series. Columns can be different type. DataFrame has column and row names for easy search and access to the stored data.

DataFrame axis

Pandas DataFrame is a two dimensional table and many operations can be performed along row or columns. To specify the direction of this operation, you can use parameter axis equal 0 for rows and 1 for columns

Different ways to create Pandas DataFrame

There are a lot of different ways to create Pandas DataFrame. Here I will try to describe most important of them. Column and row names can be generated automatically, or can be declared by keywords columns and index, respectively.

Creating empty Panda DataFrames


empty_dataframe = pd.DataFrame()
print(empty_dataframe)
#Empty DataFrame
#Columns: []
#Index: []

Concatenation of series

DataFrame is a collection of Series and this is very logical, that DataFrame can be created via concatenation of few Series. During this concatenation, it is always important to remember about behaviour in the case of concatenation of different size objects


num = pd.Series([1, 2, 3])
let = pd.Series(['a', 'b', 'c', 'd'])
df1 = pd.concat([num, let], axis = 1)
#     0  1 <- This is a column names
#0  1.0  a
#1  2.0  b
#2  3.0  c
#3  NaN  d
type(df1) # 

df0 = pd.concat([num, let], axis = 0) # This is a series !!!!
#0    1
#1    2
#2    3
#0    a
#1    b
#2    c
#3    d
#dtype: object
type(df0) # 

By using constructor with series or lists to columns

It is possible to create DataFrame with constructor from series or lists with the same operation. Please note, if you use lists – they should be the same length!!!


num = pd.Series([1, 2, 3])            # can be num = [1, 2, 3]
let = pd.Series(['a', 'b', 'c', 'd']) # can be let = ['a', 'b', 'c']
df = pd.DataFrame({'numbers': num, 'letters':let})
#   numbers letters <- Columns have defined names
#0      1.0       a
#1      2.0       b
#2      3.0       c
#3      NaN       d

Creating Pandas DataFrame from lists of lists or tuples

List of lists can be converted to DataFrame with constructor. List of lists can be done with zipping of two lists


let = pd.Series(['a', 'b', 'c'])
num = pd.Series([1, 2, 3])
data = list(zip(let, num)) # make list of tuples [('a', 1), ('b', 2), ('c', 3)]

df = pd.DataFrame(data, columns = ['letter', 'number'])
#  letter  number
#0      a       1
#1      b       2
#2      c       3

Creating of DataFrame from dictionaries

In this example we will create DataFrame from a list of dictionaries. Keys will be used as a column names. And rows we will index with our names


data = [{'b': 2, 'c':3}, {'a': 10, 'b': 20, 'c': 30}]
df = pd.DataFrame(data, index =['one', 'two'])
#      b   c     a
#one   2   3   NaN
#two  20  30  10.0

Creating of DataFrame from NumPy array

DataFrame can be converted from 2 dimensional NumPy array. And also we will give names for rows and columns


na = np.array([[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd']]) # this is our 2D NumPy array
#array([['1', 'a'],
#       ['2', 'b'],
#       ['3', 'c'],
#       ['4', 'd']], dtype='< U21')
pd.DataFrame(na,
             index=['one', 'two', 'three', 'four'],
             columns = ['num', 'let'])
#      num let
#one     1   a
#two     2   b
#three   3   c
#four    4   d

Go to Page: 1; 2; 3; 4; 5; 6; 7; 8;


Published: 2021-11-05 09:11:16
Updated: 2021-12-17 02:48:39

Last 10 artitles


9 popular artitles

© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.