![](/img/trans.png)
[英]find first column elements in entire data frame and return first column values in front of each row(Pandas)
[英]Python using pandas search entry just in the first column return values of entire row
我是python的新手,无法弄清楚大熊猫。 我尝试了整个晚上,但无法正常工作。 这可能是一个重复的问题,但是我进行了搜索,但仍然没有解决。
df = pd.read_csv(r'E:\Programming\Pipeline\Tests\vfxdatasheet.csv')
df2 = df.columns.get_values()
print (df2)
给我我的专栏。 到现在为止还挺好。 我想高效地在第一列“ Shot#”中搜索条目。 如果找到该条目,则返回其整行的信息(作为列表或其他内容)
还有加分点:如何返回在特定行/列中找到的值
感谢您的帮助。 :)
编辑:
shotid = '001_0010'
ix = df['Shot#'].loc[df['Shot#'].str.contains(shotid)].index
print (ix)
导致我昨天一直遇到一个关键错误。 我正在使用WinPython,pandas包可能有问题吗?
编辑2:好的,我知道为什么它不起作用。 创建数据框时未设置分隔符。 愚蠢的错误!
df = pd.read_csv(r"E:\Programming\Pipeline\Tests\vfxdatasheet.csv", sep=';', encoding='utf-8')
Traceback (most recent call last):
File "E:/Programming/Pipeline/Python/test.py", line 8, in <module>
ix = df['Shot#'].loc[df['Shot#'].str.contains(shotid)].index
File "C:\WinPython\python-3.5.4.amd64\lib\site-packages\pandas\core\frame.py", line 2139, in __getitem__
return self._getitem_column(key)
File "C:\WinPython\python-3.5.4.amd64\lib\site-packages\pandas\core\frame.py", line 2146, in _getitem_column
return self._get_item_cache(key)
File "C:\WinPython\python-3.5.4.amd64\lib\site-packages\pandas\core\generic.py", line 1842, in _get_item_cache
values = self._data.get(item)
File "C:\WinPython\python-3.5.4.amd64\lib\site-packages\pandas\core\internals.py", line 3843, in get
loc = self.items.get_loc(item)
File "C:\WinPython\python-3.5.4.amd64\lib\site-packages\pandas\core\indexes\base.py", line 2527, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
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)
如果要从特定列返回值,可以使用多种方法:
方法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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.