Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 解析返回为列表_Python_Pandas_Formatting - Fatal编程技术网

Python 解析返回为列表

Python 解析返回为列表,python,pandas,formatting,Python,Pandas,Formatting,我运行以下代码: df = pd.read_excel(excel_file, columns = ['DeviceNumber','DeviceAddress','DeviceCity','DeviceState','StoreNumber','StoreName','DeviceConnect','Keys']) df.index.name = 'ID' def srch_knums(knum_search): get_knums = df.loc[df['DeviceNumber

我运行以下代码:

df = pd.read_excel(excel_file, columns = ['DeviceNumber','DeviceAddress','DeviceCity','DeviceState','StoreNumber','StoreName','DeviceConnect','Keys'])
df.index.name = 'ID'

def srch_knums(knum_search):
    get_knums = df.loc[df['DeviceNumber'] == knum_search]
    return get_knums

test = srch_knums(int(13))
print(test)
结果如下:

DeviceNumber DeviceAddress DeviceCity DeviceState StoreNumber StoreName DeviceConnect Keys ID
12     13 135 Sesame Street  Imaginary   AZ         410        Verizon     Here        On Sit
e

顺便说一句,在终点站看起来更漂亮。。。哈哈


我想做的是进行价值测试并使用它的各个方面,即在我正在创建的gui的特定部分中打印它。问题是,访问test的各种列表值的语法是什么?TBH当我在gui中显示它时,我宁愿更改标签,并且想知道如何做到这一点,例如,进行测试[0],它应该是设备编号(13)的值,并且能够将其分配给变量。也就是说,制作一个标签,上面写着“kiosk number”,然后在旁边打印一个变量分配测试[0],等等。因为我宁愿自己格式化它,而不是从返回的奇怪打印输出。

如果你想返回标量值,首先通过测试列
col1
和列
col2
的输出进行匹配,然后
loc
是必需的,还添加了
next
iter
,用于在不匹配时返回默认值:

def srch_knums(col1, knum_search, col2):
    return next(iter(df.loc[df[col1] == knum_search, col2]), 'no match')

test = srch_knums('DeviceNumber', int(13), 'StoreNumber')
print (test)
410
如果需要列表:

def srch_knums(col1, knum_search, col2):
    return df.loc[df[col1] == knum_search, col2].tolist()

test = srch_knums('DeviceNumber', int(13), 'StoreNumber')
print (test)
[410]

如果要返回标量值,首先通过测试列
col1
和列
col2
的输出进行匹配,然后需要
loc
,如果不匹配,还需要添加
next
iter
以返回默认值:

def srch_knums(col1, knum_search, col2):
    return next(iter(df.loc[df[col1] == knum_search, col2]), 'no match')

test = srch_knums('DeviceNumber', int(13), 'StoreNumber')
print (test)
410
如果需要列表:

def srch_knums(col1, knum_search, col2):
    return df.loc[df[col1] == knum_search, col2].tolist()

test = srch_knums('DeviceNumber', int(13), 'StoreNumber')
print (test)
[410]
更改行:

get_knums = df.loc[df['DeviceNumber'] == knum_search]

您不需要使用loc。

更改行:

get_knums = df.loc[df['DeviceNumber'] == knum_search]

你不需要使用loc