Pandas DataFrame

Pandas.DataFrame is the two dimensional array Python DataFrame
import pandas as pd 
my_dict={
	'NAME':['Ravi','Raju','Alex'],
	'ID':[1,2,3],'MATH':[30,40,50],
	'ENGLISH':[20,30,40]
	}
my_data = pd.DataFrame(data=my_dict)
print(my_data)
Output is here
   NAME  ID  MATH  ENGLISH
0  Ravi   1    30       20
1  Raju   2    40       30
2  Alex   3    50       40

Adding index

Instead of using built-in index 0,1,3 ( above code ) , we can use our own index.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex'],
         'ID':[1,2,3],'MATH':[30,40,50],'ENGLISH':[20,30,40]}
my_data = pd.DataFrame(data=my_dict)
my_data.index=[1,2,3]
print(my_data)
Output is here
   NAME  ID  MATH  ENGLISH
1  Ravi   1    30       20
2  Raju   2    40       30
3  Alex   3    50       40
We can use string as index
my_data.index=['a','b','c']
Output is here
   NAME  ID  MATH  ENGLISH
a  Ravi   1    30       20
b  Raju   2    40       30
c  Alex   3    50       40

Displaying specific columns

We used list as column names.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex'],
         'ID':[1,2,3],'MATH':[30,40,50],'ENGLISH':[20,30,40]}
my_data = pd.DataFrame(data=my_dict)
print(my_data[['NAME','ID']])
Output
   NAME  ID
0  Ravi   1
1  Raju   2
2  Alex   3
As we used List as column names , we can use all the columns and then remove columns which we don't want to display.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex'],
         'ID':[1,2,3],'MATH':[30,40,50],'ENGLISH':[20,30,40]}
my_data = pd.DataFrame(data=my_dict)
my_col_list=list(my_data) # list of column names
my_col_list.remove('ID')  # Remove ID from the list of column names
print(my_data[my_col_list])
Output is here
   NAME  MATH  ENGLISH
0  Ravi    30       20
1  Raju    40       30
2  Alex    50       40

Specific rows

import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex'],
         'ID':[1,2,3],'MATH':[30,40,50],'ENGLISH':[20,30,40]}
my_data = pd.DataFrame(data=my_dict)
print(my_data[0:2])
Output
   NAME  ID  MATH  ENGLISH
0  Ravi   1    30       20
1  Raju   2    40       30
Some more examples
print(my_data[:0]) 
Output
Empty DataFrame
Columns: [NAME, ID, MATH, ENGLISH]
Index: []
First row
print(my_data[:1])
Output
   NAME  ID  MATH  ENGLISH
0  Ravi   1    30       20

Total number of rows

import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],'MATH':[30,40,50,60,70,80],'ENGLISH':[20,30,40,50,60,70]}
my_data = pd.DataFrame(data=my_dict)
print(len(my_data.index))
Output
6

Attributes

Use this code for examples of all sample attributes shown below.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],'MATH':[30,40,50,60,70,80],'ENGLISH':[20,30,40,50,60,70]}
my_data = pd.DataFrame(data=my_dict)
print(my_data)
Python DataFrame We can print the output here
   NAME  ID  MATH  ENGLISH
0  Ravi   1    30       20
1  Raju   2    40       30
2  Alex   3    50       40
3   Ron   4    60       50
4  King   5    70       60
5  Jack   6    80       70
Above code will be used to check different attributes

T :Transpose , Changing Columns and indexes
print(my_data.T)
            0     1     2    3     4     5
NAME     Ravi  Raju  Alex  Ron  King  Jack
ID          1     2     3    4     5     6
MATH       30    40    50   60    70    80
ENGLISH    20    30    40   50    60    70
at : at value by row, column pair
print(my_data.at[3,'ENGLISH']) # 50 
columns: Name of the Columns
print(my_data.columns) #Index(['NAME', 'ID', 'MATH', 'ENGLISH'], dtype='object')
dtypes : dtypes of used DataFrame
print(my_data.dtypes)
Output
NAME       object
ID          int64
MATH        int64
ENGLISH     int64
dtype: object
empty : The DataFrame empty or not ( True or False )
print(my_data.empty) # False 
iat : Value at position at rows and columns as integers.
print(my_data.iat[2,3]) # 40
iloc Values at different Positions , More on iloc
index : Details on row labels
print(my_data.index) # RangeIndex(start=0, stop=6, step=1)
is_copy : deprecated ix : deprecated , position based on row and column
print(my_data.ix[2,'MATH']) # 50 
loc : Values , More on loc
ndim : array dimensions or axes
print(my_data.ndim) #2
shape : Tuple giving dimension of DataFrame
print(my_data.shape) # (6,4)
size : Number of elements in the array
print(my_data.size) #24
style : Associated html style
print(my_data.style)
values : All values of the DataFrame
print(my_data.values)
Output is here
[['Ravi' 1 30 20]
 ['Raju' 2 40 30]
 ['Alex' 3 50 40]
 ['Ron' 4 60 50]
 ['King' 5 70 60]
 ['Jack' 6 80 70]]
Filtering of Data


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer