1. 程式人生 > >USA gov data from Bitly

USA gov data from Bitly

資料處理中使用函式解析:

json.load()與json.loads()的區別

json.loads() #將json string解析為python dictionary
json.load()  #將json object解析為python dictionary

簡單示例: 簡單示例

dictionary預設值設定

my_dict.setdefault('a',[]) #將my_dict['a']預設值設為list
my_dict = dict() #如果不初始化直接引入my_dict['a']會提示出錯
my_dict = collections.defaultdict(list
) #將my_dict的各個key的value預設為list

簡單示例: 簡單示例 簡單示例 簡單示例 簡單示例

collections.Counter(iterable)

from collections import Counter
Counter(list) #counting the number of elements of list,return Counter dictionary object
Counter(dictionary) #return Counter dictionary object
#Counter() support three methods:
Counter.elements
() #根據Counter dictionary中value值,返回Counter key Counter.most_common(n=10) #返回最常見的n個(key,value) Counter1.subtract(Counter2) #返回Counter1 - Counter2的差值

簡單示例: 簡單示例

list.sort()與sorted()的區別

list.sort(reverse=True) #sort()是應用在list上的方法
sorted(iterable,key,reverse=True) #sorted()是應用在所有可迭代物件上的,返回一個新的iterable

簡單示例: 簡單示例 簡單示例

series.notnull()

簡單示例: 簡單示例

series.str

series.str #返回的是pandas string_object,其具有一些StringMethods
series.str.split('')
series.str.replace('o','t')#將series中的o用t代替,返回為series
series.str.contains('a')#series各個元素是否包含a,返回boolean series

series.nlargest(10)

簡單示例: 簡單示例

資料分析程式碼