python辣雞小函數

#datetime.time()轉換second
def t2s(t):
    h = t.hour
    m = t.minute
    s = t.second
    return int(h) * 3600 + int(m) * 60 + int(s)


#second轉換成時間
def s2t(seconds):
    m, s = divmod(seconds, 60)
    h, m = divmod(m, 60)
    return "%02d:%02d:%02d" % (h, m, s)

特別關注‘divmod’函數!

 

#刪除某列含有特殊數值的行
df1=df1[~df1['A'].isin([1])]
#通過~取反,選取不包含數字1的行

Datafram刪除行

#例子
odata.drop(odata.index[[16,17]],inplace=True) #如果inplace=True則原有數據塊的相應行被刪除,默認False
#對列表中的類別進行計數
def count_num(li):
    tp_dic = {}
    for i in li:
        if i not in tp_dic:
            tp_dic.setdefault((i),1)
        else:
            tp_dic[i] += 1
    return tp_dic

#對計數後的字典,根據數量由大到小排列
def sort_dic(dic):
    temp_li = sorted(dic.items(),key = lambda item:-item[1])
    return temp_li

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章