Ainotes dataframe
Ainotes dataframe
Syntax
import pandas as pd
<dataFrameObject>=pd.DataFrame(data=None, index=None, columns=None,
dtype=None, copy=None)
DataFrame object can be created by passing a numpy array, dictionary and list.
Dataframe df:
# Creating above dataframe using dictionary value as list
import pandas as pd
d={'name':['avi','ravi','savi','kavi'],'age':[25,35,45,55],'salary':[3000,4500,2500,6000]}
df=pd.DataFrame(d,index=['e1','e2','e3','e4'])
print(df)
import pandas as pd
Df.loc[‘e2’,’name’] or df.iloc[1:0]
axis=0 will delete row and axis=1 will delete the column.
Dataframe df:
#deleting rows
df.drop(['e1','e3'],inplace=True)
#deleting columns
df.drop(['age','name'],axis=1,inplace=True)
>>>df.index
Index(['e1', 'e2', 'e3', 'e4'], dtype='object')
>>>df.columns
Index(['name', 'age', 'salary'], dtype='object')
>>>df.dtypes
name object
age int64
salary int64
dtype: object
>>>df.values
[['avi' 25 3000]
['ravi' 35 4500]
['savi' 45 2500]
['kavi' 55 6000]]
>>>df.shape
(4, 3)
>>>df.head(2)
name age salary
e1 avi 25 3000
e2 ravi 35 4500
>>>df.tail(2)
name age salary
e3 savi 45 2500
e4 kavi 55 6000
CSV File
A CSV file (Comma Separated Values file) is a type of plain text file that
uses specific structuring to arrange tabular data. CSV files are normally
created by programs that handle large amounts of data. They are a
convenient way to export data from spread sheets and databases as well
as import or use it in other programs.
Writing in csv file or Storing DataFrame’s Data to CSV file: to_csv()
function is used.
Syntax:-
df.to_csv(“file path”)
Example:
import pandas as pd
d={'PRODUCT ID':[1001,1002,1003,1004,1005],
'PRODUCT NAME':['PEN','PENCIL','BOOK','ERASER','MARKER']}
df=pd.DataFrame(d)
df.to_csv("product.csv")
OUTPUT
OUTPUT