2017-04-03 75 views
0

如何將同一數據幀中的兩列相乘?我的數據框看起來像下面的圖像,我想輸出像這樣。但是,我無法找到如何乘以依賴於同一數據幀第一行的兩列。我非常感謝這方面的幫助。熊貓:乘以依賴於第三列的相同數據幀的兩列

request       totalbytes 
/login        8520 
/shuttle/countdown/     7970 
/shuttle/countdown/liftoff.html  0 

enter image description here

到目前爲止,我的輸出低於,但我怎樣才能得到唯一的行。 enter image description here

+1

要乘兩列,只需乘他們:'DF [ 'bytesbytes'] * DF [ 'bytesfrequency']'。但是,您的預期結果不是兩列的乘積。請解釋你想要的。對第一行的引用特別令人費解。 – DyZ

+0

我不遵循你的問題。首先你的標題與你的問題不同。其次,就我所知,您所需的輸出看起來是正確的;第三,顯然你的*實際*期望的結果是完全不同的(相乘與獲得獨特的元素)。你可以直觀地乘以列,並且只需要獲得唯一的行,那裏就有足夠的資源。請澄清你的問題。謝謝。 – spicypumpkin

+0

@Posh_Pumpkin:我得到了重複值,如我的第二張圖片所示。我如何獲得每個唯一URL的總字節數。感謝您的幫助。 – jubins

回答

1

既然你解釋你想要的...你居然想刪除重複項:

(df['bytesbytes']*df['bytesfrequency']).drop_duplicates() 
3

看來需要簡單的多列:

df['totalbytes'] = df['bytesbytes']*df['bytesfrequency'] 

或者使用mul

df['totalbytes'] = df['bytesbytes'].mul(df['bytesfrequency']) 

樣品:

df = pd.DataFrame({'bytesbytes':[3985,1420,0,0], 
        'bytesfrequency':[2,6,2,2]}) 


df['totalbytes'] = df['bytesbytes']*df['bytesfrequency'] 
print (df) 
    bytesbytes bytesfrequency totalbytes 
0  3985    2  7970 
1  1420    6  8520 
2   0    2   0 
3   0    2   0 

但也許需要groupby通過第一列request,並使用transform用於創建新Series其是多個(兩列由transform轉換,也許只需要一個):

df = pd.DataFrame({ 'request':['a','a','b','b'], 
        'bytesbytes':[3985,1420,1420,0], 
        'bytesfrequency':[2,6,6,2]}) 


g = df.groupby('request') 

print (g['bytesbytes'].transform('first')) 
0 3985 
1 3985 
2 1420 
3 1420 
Name: bytesbytes, dtype: int64 

print (g['bytesfrequency'].transform('first')) 
0 2 
1 2 
2 6 
3 6 
Name: bytesfrequency, dtype: int64 

df['totalbytes'] = g['bytesbytes'].transform('first')*g['bytesfrequency'].transform('first') 
print (df) 
    bytesbytes bytesfrequency request totalbytes 
0  3985    2  a  7970 
1  1420    6  a  7970 
2  1420    6  b  8520 
3   0    2  b  8520 

編輯:

如果需要通過request列刪除重複:

df = pd.DataFrame({ 'request':['a','a','b','b'], 
        'bytesbytes':[3985,1420,1420,0], 
        'bytesfrequency':[2,6,6,2]}) 

print (df) 
    bytesbytes bytesfrequency request 
0  3985    2  a 
1  1420    6  a 
2  1420    6  b 
3   0    2  b 

單線解決方案 - drop_duplicates,多重和最後drop列:

df = df.drop_duplicates('request') 
     .assign(totalbytes=df['bytesbytes']*df['bytesfrequency']) 
     .drop(['bytesbytes','bytesfrequency'], axis=1) 
print (df) 
    request totalbytes 
0  a  7970 
2  b  8520 
df = df.drop_duplicates('request') 
df['totalbytes'] = df['bytesbytes']*df['bytesfrequency'] 
df = df.drop(['bytesbytes','bytesfrequency'], axis=1) 
print (df) 
    request totalbytes 
0  a  7970 
2  b  8520 
+0

謝謝。我如何獲得所有獨特請求的總字節數。所以我只需要兩列:uniquerequests和totalbytes。正如我在代碼塊中的問題所示。非常感謝您的幫助。 – jubins

+0

我認爲需要最後一個答案 - 'drop_duplicates',多個和最後一個'drop'列。 – jezrael

0

請編輯您的標題,因爲它是非常誤導

另外,要回答你的問題,pandas有一個方便的drop_duplicates方法。我強烈建議你檢查一下。

簡而言之,該方法逐字刪除所有重複行並返回一個新的DataFrame。或者,您可以使該方法僅考慮某些行 - 可以在文檔中找到詳細信息。

在你的情況,你可以簡單地做:

df2 = df2.drop_duplicates()[['requests', 'totalbytes']] 

列的索引是完全可選的,但是我加入他們,因爲我想你在你的最終輸出只想這兩列。

1

短的辦法讓你發佈預期的結果

df.drop_duplicates().set_index('request').prod(1).reset_index(name='totalbytes') 

          request totalbytes 
0    /shuttle/countdown  7970 
1       /login  8520 
2 /shuttle/countdown/liftoff.html   0