Posts

Geo-Spatial plots in python

Image
Python library Geopandas used for plotting geo-spatial data- import geopandas world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres')) world.plot() <matplotlib.axes._subplots.AxesSubplot at 0x2fd60a9ecf8>

Date TIme in Python for data scientists

Image
python date datatype- datetime pandas date datatype- Timestamp 1) creating pandas datframe with string dates value ## datetime date_data= pd.DataFrame(np.random.randint(10,100, (4,3)), columns=['A','B','C']) dates= ['2 june 2013', '5 Aug 2015', '2015-07-09', '7/12/2014'] date_data.index= dates A B C 2 june 2013 52 61 89 5 Aug 2015 21 69 89 2015-07-09 88 23 13 7/12/2014 43 39 21 creating string into pandas datetime format- date_data.index= pd.to_datetime(date_data.index) date_data A B C 2013-06-02 59 93 77 2015-08-05 33 15 28 2015-07-09 63 19 25 2014-07-12 29 36 92 2) Time difference in pandas- ...

Pivot table in Python

create a pandas dataframe- import numpy as np import pandas as pd cars_data= pd.DataFrame([[10,5,3,'X'], [1,2,3,'Y'],[3,4,5,'X'], [10,5,3,'X'], [1,2,3,'Y'],[3,4,5,'X']], columns=['A','B','C','Type']) cars_data A B C Type 0 10 5 3 X 1 1 2 3 Y 2 3 4 5 X 3 10 5 3 X 4 1 2 3 Y 5 3 4 5 X create pivot_table a method in pandas # using pivot to convert rows to column pivot_car= cars_data.pivot_table(columns=['Type', 'A'], aggfunc= [np.mean, np.amax]) mean amax Type A B X 3 4 4 10 5 5 ...

Sort Python Dictionary by Values/Keys

A-  create a dictionary- items_count = {' are': 1, 'hi': 2, 'me': 3, 'you': 5} B- Sort by values of dict - sorted_x = sorted(items_count.items(), key=lambda kv: kv[1], reverse= True) Sorted_x [('you', 5), ('me', 3), ('hi', 2), ('are', 1)] #op sorted()  have a  key  parameter to specify a function to be called on each list element prior to making comparisons. by default ascending oreder, if reverse = True then reverse order. items_count.items()- dict_item data type that items_count as tuples. kv: kv[1] function that takes 2nd value from each tuple so that comparison happens for values not keys. kv:kv[0] would have sorted by keys of dictionary. C- convert list to dictionary again- import collections sorted_dict = collections.OrderedDict(sorted_x) OrderedDict([('you', 5), ('me', 3), ('hi', 2), ('are', 1)]) #op creates dictionary from list to have dictionary ...