In [1]:
import numpy as np
import pandas as pd
In [3]:
# this is day 4 of data science
In [5]:
df=pd.DataFrame(np.random.rand(5,4),index=["a",'b','c','d','e'],columns=["si.no","Name","Marks","Grade"])
df
Out[5]:
In [6]:
# If you want to print only one column
In [8]:
df['Name']
Out[8]:
In [9]:
#If you want to print 2 columns
In [10]:
df[["si.no","Name"]]
Out[10]:
In [11]:
#in the above you have 2 brackets, if you print more than one column then you will use 2 brackets
In [12]:
#If you want to print a row
In [13]:
df.loc['b']
Out[13]:
In [14]:
#LOC commnds help in printing rows
In [15]:
#in the below example you have 2 brackets, if you print more than one row then you will use 2 brackets
In [16]:
df.loc[['a','c']]
Out[16]:
In [17]:
#if you want to print onl one column for multiple rows
In [18]:
df.loc[['a','c']]["Name"]
Out[18]:
In [19]:
#in the below example you have 2 brackets, if you print more than one column for multiple rows then you will use 2 brackets
In [20]:
df.loc[['a','c']][["Name","Grade"]]
Out[20]:
In [21]:
df.loc[['a']]["Name"]
Out[21]:
In [22]:
#to print with index
In [24]:
df.iloc[1:3,2:4]
Out[24]:
In [25]:
#in the above example, 1:3 is for rows, so it will take b and c as 3-1 is 2
In [27]:
df.iloc[:,0:1]
Out[27]:
In [29]:
df.drop("Marks",axis=1)
Out[29]:
In [30]:
#in the above example we are deleting Column Marks, so we are using axis=1
In [31]:
df
Out[31]:
In [32]:
df.drop("Marks",axis=1,inplace=True)
In [33]:
df
Out[33]:
In [34]:
#if you need permanently drop Marks, then use the command inplace=True
In [35]:
df.drop('d',axis=0,inplace=True)
In [36]:
df
Out[36]:
In [37]:
#if we need to add new column to the table above
In [38]:
df["City"]=[11,22,33,44]
df
Out[38]:
In [39]:
df["id"]=df["Grade"]+df["City"]
In [40]:
df
Out[40]:
In [41]:
# to add a row
In [42]:
df.loc["f"]=[10,20,30,40,50]
df
Out[42]:
In [43]:
df[df>.5]
Out[43]:
In [47]:
df1=pd.DataFrame({'A':[1,2,np.nan],'B':[5,np.nan,np.nan],'C':[1,2,3]})
df1
Out[47]:
In [48]:
df1
Out[48]:
In [49]:
#how to fill this Not a Number values
In [50]:
df1.fillna("Nitin")
Out[50]:
In [51]:
#to fill Nan Values in a single column
In [52]:
df1["A"].fillna("ABC")
Out[52]:
In [53]:
#to fill nan Values by the mean of the remaining numbers
In [54]:
df1["A"].fillna(df1["A"].mean())
Out[54]:
In [55]:
#to convert into CSV
In [56]:
df.to_csv("Testcsv1.csv")
In [57]:
#To read a csv file
In [58]:
df2=pd.read_csv('student_grades.csv')
In [59]:
df2
Out[59]:
In [60]:
df2.head()
Out[60]:
In [61]:
df2.head(8)
Out[61]:
In [62]:
#in the above 2 example, if you need specific no. of roads to be printed, you use head command
In [63]:
df2.tail()
Out[63]:
In [64]:
#above example will give you last 5 entries of your sheet
In [65]:
df2.info()
In [ ]:
No comments:
Post a Comment