Python3 pandas loc匹配,无法识别。 (按键错误)

Python3 pandas loc matching, It is not recognized. (KeyError)

有df,

   Remarks  Unnamed: 13  Unnamed: 14                 Unnamed: 15  
0   ttttttt            3        3.333  =10000/(10000-(h2+i2)*100)   
1   ttttttt            3        3.300                               
2   ttttttt            3        3.333   

和kwargs

kwargs = {'Unnamed: 13': '3',
          'Unnamed: 15': ''}

这个没问题。

print(df[(df["Unnamed: 13"] == "3") & (df["Unnamed: 15"] == "")])

和结果

 Remarks  Unnamed: 13  Unnamed: 14 Unnamed: 15                       _id  \
1   ttttttt            3        3.300              5ae21c969268ff4118df7f8b   
2   ttttttt            3        3.333              5ae21c969268ff4118df7f8c   

我做了这个表情

find_key_and_val =str(' & '.join(["(df["+"\""+key+"\"" + "] == " + "\"" + val + "\")" for key, val in kwargs.items()]))

打印(find_key_and_val)

(df["Unnamed: 13"] == "3") & (df["Unnamed: 15"] == "")

然后我应用了它。

print(df[find_key_and_val])

这将导致以下错误:

Traceback (most recent call last):
  File "C:\Users\tlsdy\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexes\base.py", line 2525, in get_loc
    return self._engine.get_loc(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: '(df["Unnamed: 13"] == "3") & (df["Unnamed: 15"] == "")'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/python_project_ab/amazon/property.py", line 201, in <module>
    print(df[find_key_and_val])
  File "C:\Users\tlsdy\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 2139, in __getitem__
    return self._getitem_column(key)
  File "C:\Users\tlsdy\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 2146, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users\tlsdy\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\generic.py", line 1842, in _get_item_cache
    values = self._data.get(item)
  File "C:\Users\tlsdy\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\internals.py", line 3843, in get
    loc = self.items.get_loc(item)
  File "C:\Users\tlsdy\AppData\Local\Programs\Python\Python36\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: '(df["Unnamed: 13"] == "3") & (df["Unnamed: 15"] == "")'

                 

KeyError: '(df["Unnamed: 13"] == "3") & (df["Unnamed: 15"] == "")'

我该怎么办?

您试图让 Pandas 执行涉及一系列数据帧切片的复杂操作,但您给它的实际上只是一串字符。

这个表达式是对数据帧的一系列切片操作:

print(df[(df["Unnamed: 13"] == "3") & (df["Unnamed: 15"] == "")])

虽然这个表达式是一堆字符:

str(' & '.join(["(df["+"\""+key+"\"" + "] == " + "\"" + val + "\")" for key, val in kwargs.items()]))

你的数据框实际上是在寻找标签为“(df["Unnamed: 13"] == “3”) & (df["Unnamed: 15"] == “”) 的键,这是不存在的。编译器不会遇到这个中间程序并认为 "Oh, he means this to be code he wants executed"。它只是将其视为与其他字符串一样的字符串。

如果你想把一个字符串作为一个命令来执行,你可以使用eval()方法。例如:

import pandas as pd

data = [
    ['ttttttt', 3, 3.333, 10.0],
    ['ttttttt', 3, 3.300, ""],
    ['ttttttt', 3, 3.333, ""],
]

df = pd.DataFrame(data, columns=['Remarks', 'Unnamed: 13', 'Unnamed: 14', 'Unnamed: 15'])
string_query = """df[(df['Unnamed: 13'] == 3) & (df["Unnamed: 15"] == "")]"""

print(eval(string_query))

输出:

   Remarks  Unnamed: 13  Unnamed: 14 Unnamed: 15
1  ttttttt            3        3.300            
2  ttttttt            3        3.333