User Tools

Site Tools


tutorials:python_quick_reference

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
tutorials:python_quick_reference [2021/05/01 20:44]
marcus.williams [Github]
tutorials:python_quick_reference [2021/08/03 16:07]
marcus.williams [Data interrogation]
Line 105: Line 105:
 <​code>​ <​code>​
 pt_structure = pd.pivot_table(df_structure,​ pt_structure = pd.pivot_table(df_structure,​
-    '​OBJECTID',​+    ​values='​OBJECTID',​
     index=['​propType'​],​     index=['​propType'​],​
     columns=['​strucType'​],​     columns=['​strucType'​],​
     margins=True,​     margins=True,​
-    aggfunc=len)+    aggfunc=np.sum)
 </​code>​ </​code>​
 [[http://​pandas.pydata.org/​pandas-docs/​stable/​generated/​pandas.pivot_table.html#​pandas.pivot_table|ref]] | [[http://​pandas.pydata.org/​pandas-docs/​stable/​generated/​pandas.pivot_table.html#​pandas.pivot_table|ref]] |
Line 172: Line 172:
 df.reset_index() df.reset_index()
 </​code>​ </​code>​
 +
 +Remove an unwanted level
 +<​code>​
 +df.columns = df.columns.get_level_values('​CEUDEndUse_res'​)
 +</​code>​
 +
 ===== Dropping/​deleting data ===== ===== Dropping/​deleting data =====
  
Line 417: Line 423:
 ===== Handy Matrix & Vector Operations ===== ===== Handy Matrix & Vector Operations =====
  
-**shares**+**shares ​/ norm** 
 each row sums to 1 each row sums to 1
 <​code>​ <​code>​
-df_shr = df_quantity.div(df_quantity.sum(axis=1), axis='​index'​)) +df_shr = df_quantity.div(df_quantity.sum(axis='​columns'​), axis='​index'​)) 
-</​code> ​                            ​+</​code>​ 
 + 
 +each column sums to 1 
 +<​code>​ 
 +df_shr = df_quantity.div(df_quantity.sum(axis='​index'​),​ axis='​columns'​)) 
 +</​code> ​  
 + 
 +===== Jupyter Notebook Markdown Tricks =====  
 +Links 
 +<​code>​ 
 +[Github](http://​github.com) 
 +</​code>​ 
 + 
 + 
 +Embed an image 
 +<​code>​ 
 +![title](img/​S_res_1.png) 
 +</​code>​ 
 + 
 +===== Python Debugger (PDB) =====  
 +[[https://​docs.python.org/​3/​library/​pdb.html|ref]] 
 + 
 +<​code>​ 
 +import pdb; pdb.set_trace() 
 +</​code>​ 
 + 
 +''​n''​ for next line; ''​c''​ to continue; ''​s''​ for step; ''​exit''​ for exit
 ===== Other functions ===== ===== Other functions =====
 ==== A ==== ==== A ====
Line 547: Line 580:
 <​code>​ <​code>​
 set([1,​2,​3]) - set([2,​3,​4]) set([1,​2,​3]) - set([2,​3,​4])
 +</​code>​
 +
 +See other set [[https://​www.w3schools.com/​python/​python_ref_set.asp|operations]] including:
 +<​code>​
 +set1.union(set2)
 +set1.intersection(set2)
 </​code>​ </​code>​
 ==== T ==== ==== T ====
tutorials/python_quick_reference.txt · Last modified: 2021/08/06 22:20 by marcus.williams