Pandas is an open-source, BSD-licensed Python library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language. Python with Pandas is used in a wide range of fields including academic and commercial domains including finance, economics, Statistics, analytics, etc.
Pandas is an open-source Python Library providing high-performance data manipulation and analysis tool using its powerful data structures. The name Pandas is derived from the word Panel Data – an Econometrics from Multidimensional data.
In 2008, developer Wes McKinney started developing pandas when in need of high performance, flexible tool for analysis of data.
Prior to Pandas, Python was majorly used for data munging and preparation. It had very little contribution towards data analysis. Pandas solved this problem. Using Pandas, we can accomplish five typical steps in the processing and analysis of data, regardless of the origin of data — load, prepare, manipulate, model, and analyze.
Python with Pandas is used in a wide range of fields including academic and commercial domains including finance, economics, Statistics, analytics, etc.
Key Features of Pandas
- Fast and efficient DataFrame object with default and customized indexing.
- Tools for loading data into in-memory data objects from different file formats.
- Data alignment and integrated handling of missing data.
- Reshaping and pivoting of date sets.
- Label-based slicing, indexing and sub setting of large data sets.
- Columns from a data structure can be deleted or inserted.
- Group by data for aggregation and transformations.
- High performance merging and joining of data.
- Time Series functionality.
Standard Python distribution doesn't come bundled with Pandas module. A lightweight alternative is to install NumPy using popular Python package installer, pip.
pip install pandas
If you install Anaconda Python package, Pandas will be installed by default with the following −
Windows
- Anaconda (from https://www.continuum.io) is a free Python distribution for SciPy stack. It is also available for Linux and Mac.
- Canopy (https://www.enthought.com/products/canopy/) is available as free as well as commercial distribution with full SciPy stack for Windows, Linux and Mac.
- Python (x,y) is a free Python distribution with SciPy stack and Spyder IDE for Windows OS. (Downloadable from http://python-xy.github.io/)
Linux
Package managers of respective Linux distributions are used to install one or more packages in SciPy stack.
For Ubuntu Users
sudo apt-get install python-numpy python-scipy python-matplotlibipythonipythonnotebook python-pandas python-sympy python-nose
For Fedora Users
sudo yum install numpyscipy python-matplotlibipython python-pandas sympy python-nose atlas-devel
Pandas deals with the following three data structures −
- Series
- DataFrame
- Panel
These data structures are built on top of Numpy array, which means they are fast.
Dimension & Description
The best way to think of these data structures is that the higher dimensional data structure is a container of its lower dimensional data structure. For example, DataFrame is a container of Series, Panel is a container of DataFrame.
Data Structure | Dimensions | Description |
---|---|---|
Series | 1 | 1D labeled homogeneous array, size immutable. |
Data Frames | 2 | General 2D labeled, size-mutable tabular structure with potentially heterogeneously typed columns. |
Panel | 3 | General 3D labeled, size-mutable array. |
Building and handling two or more dimensional arrays is a tedious task, burden is placed on the user to consider the orientation of the data set when writing functions. But using Pandas data structures, the mental effort of the user is reduced.
For example, with tabular data (DataFrame) it is more semantically helpful to think of the index (the rows) and the columns rather than axis 0 and axis 1.
Mutability
All Pandas data structures are value mutable (can be changed) and except Series all are size mutable. Series is size immutable.
Note − DataFrame is widely used and one of the most important data structures. Panel is used much less.
Series (C Array)
Series is a one-dimensional array like structure with homogeneous data. For example, the following series is a collection of integers 10, 23, 56, …
10 | 23 | 56 | 17 | 52 | 61 | 73 | 90 | 26 | 72 |
Key Points
- Homogeneous data
- Size Immutable
- Values of Data Mutable
DataFrame
DataFrame is a two-dimensional array with heterogeneous data. For example,
Name | Age | Gender | Rating |
---|---|---|---|
Steve | 32 | Male | 3.45 |
Lia | 28 | Female | 4.6 |
Vin | 45 | Male | 3.9 |
Katie | 38 | Female | 2.78 |
The table represents the data of a sales team of an organization with their overall performance rating. The data is represented in rows and columns. Each column represents an attribute and each row represents a person.
Data Type of Columns
The data types of the four columns are as follows −
Column | Type |
---|---|
Name | String |
Age | Integer |
Gender | String |
Rating | Float |
Key Points
- Heterogeneous data
- Size Mutable
- Data Mutable
Panel
Panel is a three-dimensional data structure with heterogeneous data. It is hard to represent the panel in graphical representation. But a panel can be illustrated as a container of DataFrame.
Key Points
- Heterogeneous data
- Size Mutable
- Data Mutable
Random Numbers in Python
Python defines a set of functions that are used to generate or manipulate random numbers. This particular type of functions are used in a lot of games, lotteries or any application requiring random number generation.
random.seed( ) in Python
random()
 function is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined.
random()
 function generates numbers for some values. This value is also called seed value.
How Seed Function Works ?
Seed function is used to save the state of random function, so that it can generate some random numbers on multiple execution of the code on the same machine or on different machines (for a specific seed value). Seed value is the previous value number generated by the generator. For the first time when there is no previous value, it uses current system time.
Using random.seed()
 function –
Here we will see how we can generate same random number every time with same seed value.
Code #1:
filter_noneedit
play_arrow
brightness_4
# random module is imported import random for i in range ( 5 ):      # Any number can be used in place of '0'.     random.seed( 0 )      # Generated random number will be between 1 to 1000.     print (random.randint( 1 , 1000 ))      |
Output:
865 865 865 865 865
Code #2:
filter_noneedit
play_arrow
brightness_4
# importing random module import random  random.seed( 3 )  # print a random number between 1 and 1000. print (random.randint( 1 , 1000 ))  # if you want to get the same random number again then, random.seed( 3 ) print (random.randint( 1 , 1000 ))  # If seed function is not used  # Gives totally unpredictable response. print (random.randint( 1 , 1000 )) |
Output:
244 244 607
# random module is imported
import random
for i in range(5):
# Any number can be used in place of '0'.
random.seed(5)
# Generated random number will be between 1 to 1000.
print(random.randint(1, 1000))
# importing random module
import random
random.seed(3)
# print a random number between 1 and 1000.
print(random.randint(1, 1000))
# if you want to get the same random number again then,
random.seed(3)
print(random.randint(1, 1000))
# If seed function is not used
# Gives totally unpredictable response.
print(random.randint(1, 1000))
On executing the above code, the above two print statements will generate a response 244 but the third print statement gives an unpredictable response.
Uses of random.seed() –
- This is used in generation of pseudo-random encryption key. Encryption keys are important part of computer security. These are the kind of secret keys which used to protect data from unauthorized access over internet.
- It makes optimization of codes easy where random numbers are used for testing. The output of the code sometime depends on input. So the use of random numbers for testing algorithm can be complex. Also seed function is used to generate same random numbers again and again and simplifies algorithm testing process.
Randon Number Operations :
1. choice() :- This function is used to generate 1 random number from a container.
2. randrange(beg, end, step) :- This function is also used to generate random number but within a range specified in its arguments. This function takes 3 arguments, beginning number (included in generation), last number (excluded in generation) and step ( to skip numbers in range while selecting).
filter_noneedit
play_arrow
brightness_4
# Python code to demonstrate the working of # choice() and randrange() # importing "random" for random operations import random # using choice() to generate a random number from a # given list of numbers. print ( "A random number from list is : " ,end = "") print (random.choice([ 1 , 4 , 8 , 10 , 3 ])) # using randrange() to generate in range from 20 # to 50. The last parameter 3 is step size to skip # three numbers when selecting. print ( "A random number from range is : " ,end = "") print (random.randrange( 20 , 50 , 3 )) |
Output:
A random number from list is : 4 A random number from range is : 41
3. random() :- This number is used to generate a float random number less than 1 and greater or equal to 0.
4. seed() :- This function maps a particular random number with the seed argument mentioned. All random numbers called after the seeded value returns the mapped number.
filter_noneedit
play_arrow
brightness_4
# Python code to demonstrate the working of # random() and seed() # importing "random" for random operations import random # using random() to generate a random number # between 0 and 1 print ( "A random number between 0 and 1 is : " , end = "") print (random.random()) # using seed() to seed a random number random.seed( 5 ) # printing mapped random number print ( "The mapped random number with 5 is : " , end = "") print (random.random()) # using seed() to seed different random number random.seed( 7 ) # printing mapped random number print ( "The mapped random number with 7 is : " , end = "") print (random.random()) # using seed() to seed to 5 again random.seed( 5 ) # printing mapped random number print ( "The mapped random number with 5 is : " ,end = "") print (random.random()) # using seed() to seed to 7 again random.seed( 7 ) # printing mapped random number print ( "The mapped random number with 7 is : " ,end = "") print (random.random()) |
Output:
A random number between 0 and 1 is : 0.510721762520941 The mapped random number with 5 is : 0.6229016948897019 The mapped random number with 7 is : 0.32383276483316237 The mapped random number with 5 is : 0.6229016948897019 The mapped random number with 7 is : 0.32383276483316237
5. shuffle() :- This function is used to shuffle the entire list to randomly arrange them.
6. uniform(a, b) :- This function is used to generate a floating point random number between the numbers mentioned in its arguments. It takes two arguments, lower limit(included in generation) and upper limit(not included in generation).
filter_noneedit
play_arrow
brightness_4
# Python code to demonstrate the working of # shuffle() and uniform() # importing "random" for random operations import random # Initializing list li = [ 1 , 4 , 5 , 10 , 2 ] # Printing list before shuffling print ( "The list before shuffling is : " , end = "") for i in range ( 0 , len (li)): print (li[i], end = " " ) print ( "\r" ) # using shuffle() to shuffle the list random.shuffle(li) # Printing list after shuffling print ( "The list after shuffling is : " , end = "") for i in range ( 0 , len (li)): print (li[i], end = " " ) print ( "\r" ) # using uniform() to generate random floating number in range # prints number between 5 and 10 print ( "The random floating point number between 5 and 10 is : " ,end = "") print (random.uniform( 5 , 10 )) |
Output:
The list before shuffling is : 1 4 5 10 2 The list after shuffling is : 2 1 4 5 10 The random floating point number between 5 and 10 is : 5.183697823553464
#############################NUMPY Array##################
import numpy as np
a=[1,2,3,4]
print(a)
b=np.array(a,float)
print(b)
print(b[2])
print(b[-2])
print(b[1:4])
print(b.size)
print(b.shape)
print(type(b))
b=np.zeros(5)
print(b)
b=np.ones(8)
print(b)
b=np.array([[1,2,3,4],[1,2,3,4]],int)
print(b)
print(b[1,1])
print(b[-1,-1])
print(b.size)
print(b.shape)
print(type(b))
b=np.zeros((2,4))
print(b)
b=np.ones((2,4))
print(b)
###########GRAPHS#######################
import matplotlib.pyplot as plt
x=[1,2,3]
y=[6,7,4]
plt.plot(x,y)
plt.show()
#######BAR CHART###############
import matplotlib.pyplot as plt
l=[1,2,3,4,5]
h=[10,24,36,40,5]
plt.bar(l,h,color=['red','blue'])
plt.show()
########PIE CHART ##################
import matplotlib.pyplot as plt
a=['eat','sleep','work','play']
s=[3,7,8,6]
c=['r','m','g','b']
plt.pie(s,labels=a,colors=c)
plt.show()
############SERIES#################
i
#Create an Empty Series
#import the pandas library and aliasing as pd
import pandas as pd
s = pd.Series()
print("s=",s)
#Create a Series from ndarray
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data)
print("s=",s)
# With index
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data,index=[100,101,102,103])
print(s)
#Create a Series from dict
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = pd.Series(data)
print(s)
# With Index
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = pd.Series(data,index=['b','c','d','a'])
print(s)
# Create a Series from Scalar
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
s = pd.Series(5, index=[0, 1, 2, 3])
print(s)
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
#retrieve the first element
print( s[0])
#Retrieve Data Using Label (Index)
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
#retrieve a single element
print( s['a'])
# Using list of indexes
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
#retrieve multiple elements
print (s[['a','c','d']])
#####################FRAMES ###########
import pandas as pd
import numpy as np
df = pd.DataFrame()
print(df)
data=[1,2,3,4,5]
df = pd.DataFrame(data)
print(df)
data=[['abc',10],['def',34],['ghi',23]]
df = pd.DataFrame(data)
print(df)
df = pd.DataFrame(data,columns=['name','age'])
print(df)
data={'name':['acd','asdc','edff'],'age':[23,32,34]}
df = pd.DataFrame(data)
print(df)
df = pd.DataFrame(data,index=['rank1','rank2','rank3'])
print(df)
########################
#####MATRIX
#matlib.empty()
#The matlib.empty() function returns a new matrix without
# initializing the entries. The function takes
#the following parameters.
#numpy.matlib.empty(shape, dtype, order)
import numpy.matlib
import numpy as np
print( np.matlib.empty((2,2)) )
print( np.matlib.zeros((2,2)) )
print (np.matlib.ones((2,2)))
#The numpy.matlib.identity() function returns the Identity
# matrix of the given size. An identity matrix is a square
# matrix with all diagonal elements as 1.
print( np.matlib.identity(5, dtype = float))
#numpy.matlib.rand()
#The numpy.matlib.rand() function returns a matrix of
#the given size filled with random values.
print( np.matlib.identity(5, dtype = float))
#The numpy.save() file stores the input array in a disk
# file with npy extension.
a = np.array([1,2,3,4,5])
np.save('outfile',a)
#To reconstruct array from outfile.npy, use load()
#function.
b = np.load('outfile.npy')
print( b )
Transpose
>>> a=numpy.array([[1,2],[3,4]])
>>> print(a)
[[1 2]
[3 4]]
>>> a.T
array([[1, 3],
[2, 4]])
>>>
######################
import numpy.matlib
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[11,12],[13,14]])
print(a)
print(b)
c=np.dot(a,b)
print(c)
# Determinant ad-bc
a = np.array([[1,2], [3,4]])
print( np.linalg.det(a))
# Statistics
a = np.array([[3,7,5],[8,4,3],[2,4,9]])
print( 'Our array is:' )
print( a )
print( '\n' )
print( 'Applying amin() function:')
print( np.amin(a,1) )
print( '\n' )
print( 'Applying amin() function again:')
print( np.amin(a,0) )
print( '\n' )
print( 'Applying amax() function:')
print( np.amax(a) )
print( '\n' )
print( 'Applying amax() function again:')
print( np.amax(a, axis = 0))
#####
#The numpy.ptp() function returns the range
# (maximum-minimum) of values along an axis.
a = np.array([[3,7,5],[8,4,3],[2,4,9]])
print( 'Our array is:' )
print( a )
print( '\n' )
print( 'Applying ptp() function:' )
print( np.ptp(a) )
print( '\n' )
print( 'Applying ptp() function along axis 1:' )
print( np.ptp(a, axis = 1) )
print( '\n' )
print ('Applying ptp() function along axis 0:')
print (np.ptp(a, axis = 0) )
#
a = np.array([[30,40,70],[80,20,10],[50,90,60]])
print( 'Our array is:' )
print( a )
print( '\n' )
print( 'Applying percentile() function:' )
print( np.percentile(a,50) )
print( '\n' )
print( 'Applying percentile() function along axis 1:')
print( np.percentile(a,50, axis = 1) )
print( '\n' )
print( 'Applying percentile() function along axis 0:' )
print( np.percentile(a,50, axis = 0))
#Median
import numpy as np
a = np.array([[30,65,70],[80,95,10],[50,90,60]])
print( 'Our array is:')
print( a )
print( '\n' )
print( 'Applying median() function:' )
print( np.median(a) )
print( '\n' )
print( 'Applying median() function along axis 0:' )
print( np.median(a, axis = 0) )
print( '\n')
print( 'Applying median() function along axis 1:' )
print( np.median(a, axis = 1))
# Mean
import numpy as np
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print ('Our array is:' )
print (a )
print( '\n' )
print( 'Applying mean() function:' )
print( np.mean(a) )
print( '\n' )
print( 'Applying mean() function along axis 0:' )
print( np.mean(a, axis = 0) )
print( '\n' )
print( 'Applying mean() function along axis 1:' )
print( np.mean(a, axis = 1))
##############RANDOM NUMBER##############
# importing "random" for random operations
import random
# using choice() to generate a random number from a
# given list of numbers.
print ("A random number from list is : ",end="")
print (random.choice([1, 4, 8, 10, 3]))
# using randrange() to generate in range from 20
# to 50. The last parameter 3 is step size to skip
# three numbers when selecting.
print ("A random number from range is : ",end="")
print (random.randrange(20, 50, 3))
#3. random() :- This number is used to generate a float
#random number less than 1 and greater or equal to 0.
#4. seed() :- This function maps a particular random
#number with the seed argument mentioned.
#All random number #s called after the seeded value
#returns the mapped number.
# using random() to generate a random number
# between 0 and 1
print ("A random number between 0 and 1 is : ", end="")
print (random.random())
# using seed() to seed a random number
random.seed(5)
# printing mapped random number
print ("The mapped random number with 5 is : ", end="")
print (random.random())
# using seed() to seed different random number
random.seed(7)
# printing mapped random number
print ("The mapped random number with 7 is : ", end="")
print (random.random())
# using seed() to seed to 5 again
random.seed(5)
# printing mapped random number
print ("The mapped random number with 5 is : ",end="")
print (random.random())
# using seed() to seed to 7 again
random.seed(7)
# printing mapped random number
print ("The mapped random number with 7 is : ",end="")
print (random.random())
D:\lab\batch83>python
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> l=[1,2,3,4]
>>> print(l)
[1, 2, 3, 4]
>>> import numpy as np
>>> a=np.array(l)
>>> print(a)
[1 2 3 4]
>>> a=np.array(l,float)
>>> print(a)
[1. 2. 3. 4.]
>>> print(a[0])
1.0
>>> print(a[-1])
4.0
>>> print(a[1:3])
[2. 3.]
>>> print(a[1:])
[2. 3. 4.]
>>> print(a[-2:])
[3. 4.]
>>> print(a.size)
4
>>> print(a.shape)
(4,)
>>> a=np.zeros(4)
>>> print(a)
[0. 0. 0. 0.]
>>> a=np.ones(4)
>>> print(a)
[1. 1. 1. 1.]
>>> b=np.array([2,3,4,5])
>>> c=np.array([2,3,4,5])
>>> b+c
array([ 4, 6, 8, 10])
>>> print(l)
[1, 2, 3, 4]
>>> a=np.array([l,l])
>>> print(l)
[1, 2, 3, 4]
>>> print(a)
[[1 2 3 4]
[1 2 3 4]]
>> a.shape
(2, 4)
>>> print(a)
[[1 2 3 4]
[1 2 3 4]]
>>> print(a[1,1])
2
>>> print(a[:,1])
[2 2]
>>> print(a[:,-2:])
[[3 4]
[3 4]]
>>> print(a[0,1])
2
>>> print(a[1,2])
3
>>> print(a[-1,1])
2
>>> print(a.size)
8
>>> a=np.zeros((2,4))
>>> print(a)
[[0. 0. 0. 0.]
[0. 0. 0. 0.]]
>>> a=np.ones((2,4))
>>> print(a)
[[1. 1. 1. 1.]
[1. 1. 1. 1.]]
>>> a=np.array([[1,2,3,4],[1,2,3,4]])
>>> b=np.array([[1,2,3,4],[1,2,3,4]])
>>> print(a+b)
[[2 4 6 8]
[2 4 6 8]]
>>> import matplotlib.pyplot as plt
>>> x=[1,2,3]
>>> y=[6,7,4]
>>> plt.plot(x,y)
[<matplotlib.lines.Line2D object at 0x0000018AA5C439D0>]
>>> plt.show()
>>> l=[1,2,3,4,5]
>>> h=[10,24,36,4,34]
>>> plt.bar(l,h,color=['red','blue'])
<BarContainer object of 5 artists>
>>> plt.show()
>>> a=['eat','sleep','work','play']
>>> s=[3,7,8,6]
>>> c=['r','m','g','b']
>>> plt.pie(s,labels=a,colors=c)
([<matplotlib.patches.Wedge object at 0x0000018AA714E580>, <matplotlib.patches.Wedge object at 0x0000018AA714EA30>, <matplotlib.patches.Wedge object at 0x0000018AA714EEB0>, <matplotlib.patches.Wedge object at 0x0000018AA715C370>], [Text(1.0162674857624154, 0.4209517756015988, 'eat'), Text(-0.14357877740594208, 1.0905893519921306, 'sleep'), Text(-0.9526279613277877, -0.5499999702695112, 'work'), Text(0.7778174593052022, -0.7778174593052025, 'play')])
>>> plt.show()
>>> import numpy.matlib as mt
>>> import numpy as np
>>> mt.empty((2,2))
matrix([[0., 0.],
[1., 1.]])
>>> mt.zeros((2,2))
matrix([[0., 0.],
[0., 0.]])
>>> mt.ones((2,2))
matrix([[1., 1.],
[1., 1.]])
>>> mt.identity(5)
matrix([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
>>> a=np.array([[1,2],[3,4]])
>>> print(a)
[[1 2]
[3 4]]
>>> print(a.T)
[[1 3]
[2 4]]
>>> b=np.array([[2,2],[2,2]])
>>> np.dot(a,b)
array([[ 6, 6],
[14, 14]])
>>> print(a)
[[1 2]
[3 4]]
>>> print(b)
[[2 2]
[2 2]]
>>>
>>> import numpy as np
>>> import numpy.matlib
>>> np.matlib.empty((2,2))
matrix([[6.80643662e+199, 1.03364647e-113],
[2.96341837e-260, 0.00000000e+000]])
>>> np.matlib.zeros((2,2))
matrix([[0., 0.],
[0., 0.]])
>>> np.matlib.ones((2,2))
matrix([[1., 1.],
[1., 1.]])
>>> np.matlib.identity(5)
matrix([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
>>> a=np.array([[1,2],[3,4]])
>>> print(a)
[[1 2]
[3 4]]
>>> b=np.array([[1,1],[2,2]])
>>> c=np.dot(a,b)
>>> print(c)
[[ 5 5]
[11 11]]
>>> c=np.linalg.det(a)
>>> print(c)
-2.0000000000000004
>>> a=np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> print(a)
[[1 2 3]
[4 5 6]
[7 8 9]]
>>> np.amin(a,0)
array([1, 2, 3])
>>> np.amax(a,0)
array([7, 8, 9])
>>> np.amax(a,axis=0)
array([7, 8, 9])
>>> np.amax(a)
9
>>> np.median(a)
5.0
>>> np.median(a,0)
array([4., 5., 6.])
>>> np.median(a,axis=1)
array([2., 5., 8.])
>>> np.amin(a,axis=1)
array([1, 4, 7])
>>> np.median(a,axis=0)
array([4., 5., 6.])
>>> np.mean(a,axis=0)
array([4., 5., 6.])
>>> print(a)
[[1 2 3]
[4 5 6]
[7 8 9]]
>>> np.mean(a,axis=1)
array([2., 5., 8.])
>>> import pandas as pd
>>> a=np.array([1,2,3,4])
>>> s=pd.Series(a)
>>> print(a)
[1 2 3 4]
>>> print(s)
0 1
1 2
2 3
3 4
dtype: int32
>>> print(s[3])
4
>>> s=pd.Series(a,index=['one','two','three','four'])
>>> print(s)
one 1
two 2
three 3
four 4
dtype: int32
>>> print(s['three'])
3
>>> d={"one":1,"two":2,"three":3,"four":4}
>>> s=pd.Series(d)
>>> print(s)
one 1
two 2
three 3
four 4
dtype: int64
>>> print(s[['three',"one"]])
three 3
one 1
dtype: int64
>>> s=pd.Series([1,2,3,4])
>>> print(s)
0 1
1 2
2 3
3 4
dtype: int64
>>> s=pd.Series()
<stdin>:1: DeprecationWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
>>> print(s)
Series([], dtype: float64)
>>> s=pd.DataFrame()
>>> df=pd.DataFrame()
>>> print(df)
Empty DataFrame
Columns: []
Index: []
>>> df=pd.DataFrame([1,2,3,4])
>>> print(df)
0
0 1
1 2
2 3
3 4
>>> df=pd.DataFrame([1,2,3,4],columns=['age'])
>>> print(df)
age
0 1
1 2
2 3
3 4
>>> df=pd.DataFrame([1,2,3,4],columns=['age'],index=['one','two','three','four'])
>>> print(df)
age
one 1
two 2
three 3
four 4
>>> df=pd.DataFrame([[1,2],[3,4]],columns=['age','roll_no'],index=['one','two'])
>>> print(df)
age roll_no
one 1 2
two 3 4
_class_helper.pxi", line 1626, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ('one', 'age')
>>> print(df)
age roll_no
one 1 2
two 3 4
>>> print(df)
age roll_no
one 1 2
two 3 4
>>> print(df.iloc[1])
age 3
roll_no 4
Name: two, dtype: int64
>>> print(df)
age roll_no
one 1 2
two 3 4
>>> print(df)
age roll_no
one 1 2
two 3 4
>>> print(df['age'])
one 1
two 3
Name: age, dtype: int64
>>> print(df['roll_no'])
one 2
two 4
Name: roll_no, dtype: int64
>>> print(df.loc['one'])
age 1
roll_no 2
Name: one, dtype: int64
>>> print(df)
age roll_no
one 1 2
two 3 4
>>> print(df)
age roll_no
one 1 2
two 3 4
>>> print(df[['roll_no','age']])
roll_no age
one 2 1
two 4 3
>>> print(df.loc['one'])
age 1
roll_no 2
Name: one, dtype: int64
>>> print(df.loc[['one','two']])
age roll_no
one 1 2
two 3 4
>>> print(df.loc['one'])
age 1
roll_no 2
Name: one, dtype: int64
>>> print(df.iloc[0])
age 1
roll_no 2
Name: one, dtype: int64
>>> print(df.iloc[[0,1]])
age roll_no
one 1 2
two 3 4
>>> print(df[['roll_no','age']])
roll_no age
one 2 1
two 4 3
>>> print(df.loc['one'])
age 1
roll_no 2
Name: one, dtype: int64
>>> quit