Skip to content
Rushikesh edited this page Jan 25, 2020 · 7 revisions
  • How to get Current path in Python

import os
cur_path = os.path.dirname(os.path.realpath(file))
csv_path = cur_path + “\\data\\” + “train.csv”


  • How to use csv column as Index

which means PasengerId from csv file would be used as index and if its not present in csv then error from python
pd.read_csv(csv_file ,
nrows=1 ,
index_col=‘PasengerId’)

Note: nrows means number of rows to be selected from csv files to panda


  • How to select desired columns from csv to pandas

df = pd.read_csv(csv_path, nrows=1, index_col=‘PassengerId’, usecols=[‘PassengerId’,‘Embarked’,‘Column1’])
Please note: Mandatory to put index column in usecols

alternatively
col_to_use = [‘PassengerId’,‘Embarked’,‘Column1’]
df = pd.read_csv(csv_path,
nrows=1,
index_col=‘PassengerId’,
usecols=col_to_use)


  • How can we save pandas data temporary ?

We can use pickle to save .
df.to_pickle(‘data_frame.pickle’)
df.from_pickle(‘data_frame.pickle’)


  • Some UseFul Utilities

pd.unique(df[‘ColumnName’]) → gives unique values for ColumnName
len(pd.unique(df[‘ColumnName’])) → gives countof unique values for ColumnName

Clone this wiki locally