Dataframe_Syntax
Dataframe_Syntax
Empty Dataframe
Df=pd.DataFrame()
o/p
Empty DataFrame
Columns:[]
Index:[]
COLUMNS
Accessing columns
DF[‘column name’]
DF[[‘col1’,’col2’,’col3’]]
Modify column values
DF[‘existing column’]=[values]
Adding new column
DF[‘new column’]=[values]
Rename Column Values
DF.rename({‘old column name’:‘new column name’, old column
name1’:‘new column name1’},axis=’columns’)
Deleting columns
DF.drop(‘column’,axis=1) for deleting column
ROWS
Adding new row
DF.loc[‘new row’]=[values]
Rename row Values
DataFrame Syntax
DF.rename({‘old row name’:‘new row name’, old row name1’:‘new row
name1’},axis=’index’)
Deleting row
DF.drop(‘row’,axis=0) for deleting row
Attributes
<DF>.index
<DF>.columns
<DF>.axis
<DF>.values
<DF>.dtypes
<DF>.size Number of elements including NaN
<DF>.ndim
<DF>.empty True/False
head and tail
DF.head(n) Print first n number of columns
DF.tail(n) Prints last n number of columns
Count and transpose
DF.count() counts and shows no of values in each column exclude NaN
DF.T changes the row elements into column elements and the vice versa.
Boolean Indexing
DF.loc[‘row label’] > ‘value’
DF.loc[: ,‘column label’]> ‘value’
Importing a CSV file to a DataFrame
DF = pd.read_csv("file path",sep =",", header=0)
To save a DataFrame to a text or csv file
DF.to_csv(‘file path’, sep=',')
If we do not want the column names and row names to be saved to the file
DF.to_csv( ‘file path’,sep = '@', header = False, index= False)