Skip to content

Instantly share code, notes, and snippets.

@shilpavijay
Last active February 20, 2018 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shilpavijay/3075f91bd8d792165acb5e840c19e9ae to your computer and use it in GitHub Desktop.
Save shilpavijay/3075f91bd8d792165acb5e840c19e9ae to your computer and use it in GitHub Desktop.
Pandas, Numpy
#If this is set to True, the axes which are reduced are left in the result as dimensions with size one
<np array>.sum(axis=1, keepdims=True)
np.vstack - stack array row-wise
#Convert to NP Array:
df = pd.read_csv(csvfile)
nparr = np.array(df)
#finding max of a dataframe
pd.DataFrame.max(df)
#selection on rows and columns:
df.loc[:3,['2003','2005']]
#index
df.set_index('Country', inplace=True)
#select row based on row name:
df.loc['rowname']
df.loc[['Assam','Karnataka'],['2003','2004']] #i.e. [[rownames],[colnames]]
#select row based on a value in the column:
df.loc[df['column_name'] == some_value_in_the_table
#fill NaN
df = df.fillna(0)
#get max of all values in the dataframe:
df.values.max()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment