我试图通过读取CSV文件来遍历pandas dataframe列...
import pandas as pd
df = pd.read_csv('vavs.csv', dtype={'trunkAdress':str}, index_col='mechTag')
for column in df[['areaServed']]:
# Select column contents by column name using [] operator
columnSeriesObj = df[column]
print('Colunm Name : ', column)
print('Column Contents : ', columnSeriesObj.values)
这将打印:
Colunm Name : areaServed
Column Contents : ['103_104' '101_105' '102' '110' '11' '114_116' '115_121' '117' '118_120']
但是为什么不打印hit
?我不明白为什么陈述不正确。我需要更改什么才能打印hit
?
if columnSeriesObj.values.any() == '11':
print('hit!')
答案 0 :(得分:0)
columnSeriesObj.values.any()
返回是否有任何元素为True。在您的情况下,columnSeriesObj.values.any()
的计算结果为True,不等于“ '11”,因此不会打印任何内容。
您可以尝试这样的事情:
val = [ii for ii in columnSeriesObj.values if ii == '11']
if val:
print('hit')
或类似的numpy:
if np.any(columnSeriesObj.values == value):
print('hit')
答案 1 :(得分:0)
您应该按照以下步骤检查bool系列上的['she', 'loves', 'you', 'yeah', 'yeah', 'yeah', 'she', 'loves', 'you', 'yeah', 'yeah', 'yeah', 'yeah', 'you', 'think', 'you', 'lost', 'your', 'love', 'well', 'i', 'saw', 'her', 'yesterday', "it's", 'you', "she's", 'thinking', 'of', 'and', 'she', 'told', 'me', 'what', 'to', 'say', 'she', 'says', 'she', 'loves', 'you', 'and', 'you', 'know', 'that', "can't", 'be', 'bad']
:
any
输出:
for column in df[['areaServed']]:
# Select column contents by column name using [] operator
columnSeriesObj = df[column]
print('Colunm Name : ', column)
print('Column Contents : ', columnSeriesObj.values)
if (columnSeriesObj.values == '11').any():
print('hit!')