Python只在第一列中使用pandas搜索项返回整行的值
我是python新手,很难理解熊猫。我试了一晚上,但没能成功。这可能是一个重复的问题,但是我搜索了它,仍然没有解决它Python只在第一列中使用pandas搜索项返回整行的值,python,pandas,Python,Pandas,我是python新手,很难理解熊猫。我试了一晚上,但没能成功。这可能是一个重复的问题,但是我搜索了它,仍然没有解决它 df = pd.read_csv(r'E:\Programming\Pipeline\Tests\vfxdatasheet.csv') df2 = df.columns.get_values() print (df2) 把我的专栏给我。到现在为止,一直都还不错。 我想高效地搜索第一列中名为“Shot#”的条目。如果找到条目,则返回其整行的信息(作为列表或其他形式) 还有一点:如
df = pd.read_csv(r'E:\Programming\Pipeline\Tests\vfxdatasheet.csv')
df2 = df.columns.get_values()
print (df2)
把我的专栏给我。到现在为止,一直都还不错。
我想高效地搜索第一列中名为“Shot#”的条目。如果找到条目,则返回其整行的信息(作为列表或其他形式)
还有一点:如何返回在特定行/列中找到的值
谢谢你帮了我一个忙。:)
编辑:
结果导致了我昨天一直犯的一个关键错误。我正在使用WinPython,熊猫包可能有问题吗
编辑2:好的,我知道为什么它不起作用。我在创建数据帧时没有设置分隔符。愚蠢的错误
df = pd.read_csv(r"E:\Programming\Pipeline\Tests\vfxdatasheet.csv", sep=';', encoding='utf-8')
回溯(最近一次呼叫最后一次):
文件“E:/Programming/Pipeline/Python/test.py”,第8行,在
ix=df['Shot#'].loc[df['Shot#'].str.contains(shotid)].index
文件“C:\WinPython\python-3.5.4.amd64\lib\site-packages\pandas\core\frame.py”,第2139行,在__
返回self.\u getitem\u列(键)
文件“C:\WinPython\python-3.5.4.amd64\lib\site packages\pandas\core\frame.py”,第2146行,在\u getitem\u列中
返回self.\u获取\u项目\u缓存(密钥)
文件“C:\WinPython\python-3.5.4.amd64\lib\site packages\pandas\core\generic.py”,第1842行,在\u get\u item\u缓存中
values=self.\u data.get(项目)
get中第3843行的文件“C:\WinPython\python-3.5.4.amd64\lib\site packages\pandas\core\internals.py”
loc=自身项目。获取loc(项目)
文件“C:\WinPython\python-3.5.4.amd64\lib\site packages\pandas\core\index\base.py”,第2527行,在get\u loc中
返回self.\u引擎。获取\u loc(self.\u可能\u cast\u索引器(键))
pandas.\u libs.index.IndexEngine.get\u loc中第117行的文件“pandas\\u libs\index.pyx”
pandas.\u libs.index.IndexEngine.get\u loc中第139行的文件“pandas\\u libs\index.pyx”
pandas.\u libs.hashtable.PyObjectHashTable.get\u项中的文件“pandas\\u libs\hashtable\u class\u helper.pxi”,第1265行
pandas.\u libs.hashtable.PyObjectHashTable.get\u项目中第1273行的文件“pandas\\u libs\hashtable\u class\u helper.pxi”
KeyError:“Shot#”
您可以这样尝试:
# sample data
df = pd.DataFrame({'Shot#': ['001_0010','002_0020','003_0010','003_0020','003_0030','004_0010','003_0010'],
'play': ['a','b','c','d','a','b','d']})
# let's say
val_to_search = '003_0010'
# get row index value where match is found
ix = df['Shot#'].loc[df['Shot#'].str.contains(val_to_search)].index
# get rows of match value as output
df.values[ix]
# output
array([['003_0010', 'c'],
['003_0010', 'd']], dtype=object)
如果要从specfic列返回值,可以使用多种方法:
方法1:
df.apply(lambda row: row['Shot#'] if row['Shot#'] == val_to_search else np.nan, axis=1)
方法2:
mask = df['Shot#'].str.contains(val_to_search)
df['new_col'] = df.loc[mask,'Shot#']
print(df)
Shot# play new_col
0 001_0010 a NaN
1 002_0020 b NaN
2 003_0010 c 003_0010
3 003_0020 d NaN
4 003_0030 a NaN
5 004_0010 b NaN
6 003_0010 d 003_0010
你试过什么?你写道你整个晚上都在尝试——你是如何寻找价值的?您计划如何在找到时知道其行的索引?在给定索引的情况下,您将如何写出相应的行?谢谢您,到目前为止,我编辑了我的第一篇文章,其中包含了我一直遇到的关键错误。知道吗?我只是没有正确设置csv分隔符,对不起,谢谢。
df.apply(lambda row: row['Shot#'] if row['Shot#'] == val_to_search else np.nan, axis=1)
mask = df['Shot#'].str.contains(val_to_search)
df['new_col'] = df.loc[mask,'Shot#']
print(df)
Shot# play new_col
0 001_0010 a NaN
1 002_0020 b NaN
2 003_0010 c 003_0010
3 003_0020 d NaN
4 003_0030 a NaN
5 004_0010 b NaN
6 003_0010 d 003_0010