Pandas DataFrame


Pandas

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
Here we used one Dictionary to create one DataFrame. We can also use Numpy Ndarray to create DataFrame.

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

Datframe Columns

cols=my_data.columns # list with column names
print(cols)
print(cols[2]) # specific column name
for i in cols: # listing all columns
    print(i)

Displaying specific column values

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

Creating DataFrame from String

We will use split() to create a list first. Then using the list we will create a DataFrame.
import pandas as pd 
str1='Welcome to plus2net python section'
my_list=str1.split(' ')
#print(my_list)
df=pd.DataFrame(data=my_list,columns=['words'])
print(df)
Output is here
      words
0   Welcome
1        to
2  plus2net
3    python
4   section
Using StringIO
from io import StringIO
import pandas as pd
str1 = StringIO("""col1;col2;col3
    1;5.4;Geek
    2;43.25;Ravi
    3;41.7;Ron
    4;34.2;Alex
    """)
df = pd.read_csv(str1, sep=";")
print(df)
Output
   col1   col2  col3
0     1   5.40  Geek
1     2  43.25  Ravi
2     3  41.70   Ron
3     4  34.20  Alex
DataFrame Atributes DataFrame Methods
Pandas Plotting graphs Filtering of Data Sample student DataFrame

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





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