将数据帧列与pandas python中的单个值进行比较

时间:2015-04-03 20:47:11

标签: python pandas dataframe

我使用输入的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().

提前致谢!

1 个答案:

答案 0 :(得分:1)

如果你想用for循环来做,循环遍历dfs索引:

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

但为什么不呢:

dframedesc[dframedesc['col_to_compare'] < dframelines]