-
Notifications
You must be signed in to change notification settings - Fork 1
Pandas
- 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