簡體   English   中英

將數據框列與Pandas Python中的單個值進行比較

[英]Comparing dataframe column with single value in pandas python

我使用輸入的SQL查詢在python上構建了數據框架。 之后,我為列命名,並確保用NaN值隔離列是很好的:

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())

當我嘗試將dframedesc與dframelines比較時,出現錯誤

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().

提前致謝 !

如果要使用forloop進行操作,請遍歷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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM