pandas與表處理 頂 原 薦

查詢寫入操作

pandas可以類似sql一樣有強大的查詢功能,而且寫法簡單:

print tips[['total_bill', 'tip', 'smoker', 'time']]
#顯示'total_bill', 'tip', 'smoker', 'time'列,功能類似於sql中的select命令

print tips[tips['time'] == 'Dinner']
#顯示time列中等於Dinner的數據,功能類似於sql中的Where命令

print tips[(tips['size'] >= 5) | (tips['total_bill'] > 45)]
print tips[(tips['time'] == 'Dinner') & (tips['tip'] > 5.00)]
# |功能類似於sql中的or命令, &功能類似於sql中的and命令

#index和label查詢
df.iloc[i:j,k:p]#iloc操作index,輸出第i行到第j行和第k列和第p列中的數值
df.loc['20130102':'20130104',['A','B']]#loc操作label,輸出行爲'20130102':'20130104',列爲'A','B'
df.at[dates[0],'A']#返回特定行label和列label的數值

#map函數操作
df['Oid'] = df['Name'].map(lambda x: int(x.split(' - ')[0]))

#刪除列
del df['smoker']
#增加列
df['smoker'] = np.nan
#刪除行
df = df.drop([i for i in range(1,100)],axis=0)#刪除100行
#增加行
df = df.append(pd.DataFrame(
index=[i for i in range(100,200)],columns=df.columns),ignore_index=True)#增加一百行

使用pandas寫一個將一維關係表寫成展二維開式關係表,代碼如下:

def one2two(filepath,col_value):
    '''
    該關係表爲一個Oid字段和一個Did的字段,兩個字段對應一個數字co_value,該函數將Oid和Did
    字段中數值轉換成一個以Oid爲列,Did爲行的二維數據表。
    '''
    df = pd.read_csv(filepath)
    newdf = pd.DataFrame(columns=df['Oid'].unique(),index=df['Did'].unique())
    time = len(newdf.index)
    for i in newdf.index:
        for c in newdf.columns:
            #通過查詢獲得Oid和Did對應的值
            value = df[df.Did==c][df[df.Did==c].Oid==i]
            newdf[c][i] = value[col_value]
        time=time-1
        print 'Ater %d the app will leave.'%time
    print 'Ready to write.'
    newdf.to_csv(col_value+'.csv')
    print 'Finsh write, the %s.cvs was generated'%col_value

pandas除了查詢不錯在bigfile處理也相當可觀,如下面從一個大文件中提取要素保存的函數:

def save(pathfile,outPath):
    reader = pd.read_csv(pathfile,iterator=True)#使用iterator,使pandas可以分開讀取文件
    loop = True
    chunkSize = 1000000
    chunks = []
    while loop:
        try:
            #劃分成chunksize行大小的塊進行讀取
            df = reader.get_chunk(chunkSize)
            chunks.append(df)
        except StopIteration:
            loop = False
            print 'Iteration is stopped.'

    try:
        #將塊連接起來,這裏用了一個try,因爲不知道怎麼的總是發生內存錯誤,如果不用try..finally後面
        #代碼總是無法運行,但不知道加了try..finally對數據是否有影響?
        df = pd.concat(chunks, ignore_index=True)
    finally:
        df = df[['Name','Total_length','Total_time']]
        #提出Name字段中數值中' - '之前的放入Oid中
        df['Oid'] = df['Name'].map(lambda x: int(x.split(' - ')[0]))
        df['Did'] = df['Name'].map(lambda x: int(x.split(' - ')[1]))
        del df['Name']
        df.to_csv(outPath)
        print 'Finsh.'


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