2015-04-03 43 views
0

我使用輸入SQL查詢在python上構建了一個數據框。 AFER這我的名字我的專欄,並確保它是很好的與NaN值隔離柱:在pandas python中比較dataframe列和單個值

cursor.execute(raw_input("Enter your SQL query: ")) 
records = cursor.fetchall() 
import pandas as pd 
dframesql = pd.DataFrame(records) 
dframesql.columns = [i[0] for i in cursor.description] 

問題是當我想要的行數的數據與行的數據幀總數比較後:

dframelines = len(dframesql) 
dframedesc = pd.DataFrame(dframesql.count()) 

當我試着使用dframelines比較dframedesc,我得到一個錯誤

nancol = [] 
for line in dframedesc: 
    if dframedesc < dframelines: 
     nancol.append(line) 

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). 

提前感謝!

回答

1

如果你想用一個for循環做,通過DF指數環:

nancol = [] 
for index in dframedesc.index: 
    if dframedesc.loc[index,'a_column'] < dframelines: 
     nancol.append(dframedesc.loc[index,:]) 

但是,爲什麼不只是:

dframedesc[dframedesc['col_to_compare'] < dframelines] 
相關問題