删除所有python已安装库

笔记本上安装的Python3.10.8,安装mayavi后运行代码一直提示“TypeError: unhashable type: ‘PolyData’ ”,试了很多版本都不合适。
觉得可能和已经安装的其他库有关系,想删除了重新安装。
首先,以管理员身份运行cmd,否则可能会出现拒绝访问。

1.pip freeze > requirements.txt

上述命令可以 将已安装的库信息保存到requirements.txt中
这是已安装的库:

aiofiles==23.2.1
aiohappyeyeballs==2.4.6
aiohttp==3.11.12
aiosignal==1.3.2
annotated-types==0.7.0
anyio==4.2.0
apptools==5.3.0
argon2-cffi==23.1.0
argon2-cffi-bindings==21.2.0
arrow==1.3.0
asttokens==2.4.1
async-lru==2.0.4
async-timeout==5.0.1
attrs==23.1.0
Babel==2.14.0
beautifulsoup4==4.12.2
bleach==6.1.0
certifi==2023.11.17
cffi==1.16.0
charset-normalizer==3.3.2
click==8.1.8
colorama==0.4.6
comm==0.2.0
configobj==5.0.9
contourpy==1.3.1
cycler==0.12.1
datasets==3.2.0
debugpy==1.8.0
decorator==5.1.1
defusedxml==0.7.1
dill==0.3.8
envisage==7.0.3
evaluate==0.4.3
exceptiongroup==1.2.0
executing==2.0.1
fastapi==0.115.8
fastjsonschema==2.19.0
ffmpy==0.5.0
filelock==3.17.0
fonttools==4.55.3
fqdn==1.5.1
frozenlist==1.5.0
fsspec==2024.9.0
gradio==5.15.0
gradio_client==1.7.0
h11==0.14.0
httpcore==1.0.7
httpx==0.28.1
huggingface-hub==0.28.1
idna==3.6
imageio==2.36.1
interchange==2021.0.4
ipykernel==6.27.1
ipython==8.19.0
ipywidgets==8.1.1
isoduration==20.11.0
jedi==0.19.1
jieba==0.42.1
Jinja2==3.1.2
joblib==1.4.2
json5==0.9.14
jsonpointer==2.4
jsonschema==4.20.0
jsonschema-specifications==2023.12.1
jupyter==1.0.0
jupyter-console==6.6.3
jupyter-events==0.9.0
jupyter-lsp==2.2.1
jupyter_client==8.6.0
jupyter_core==5.5.1
jupyter_server==2.12.1
jupyter_server_terminals==0.5.0
jupyterlab==4.0.9
jupyterlab-widgets==3.0.9
jupyterlab_pygments==0.3.0
jupyterlab_server==2.25.2
kiwisolver==1.4.7
markdown-it-py==3.0.0
MarkupSafe==2.1.3
matplotlib==3.10.0
matplotlib-inline==0.1.6
mayavi==4.8.2
mdurl==0.1.2
mistune==3.0.2
monotonic==1.6
mpmath==1.3.0
msgpack==1.1.0
multidict==6.1.0
multiprocess==0.70.16
nbclient==0.9.0
nbconvert==7.13.1
nbformat==5.9.2
nest-asyncio==1.5.8
networkx==3.4.2
notebook==7.0.6
notebook_shim==0.2.3
numpy==2.2.0
orjson==3.10.15
overrides==7.4.0
packaging==23.2
pandas==2.2.3
pandocfilters==1.5.0
pansi==2020.7.3
parso==0.8.3
pillow==11.0.0
platformdirs==4.1.0
prometheus-client==0.19.0
prompt-toolkit==3.0.43
propcache==0.2.1
psutil==5.9.7
pure-eval==0.2.2
py2neo==2021.2.4
pyarrow==19.0.0
pycparser==2.21
pydantic==2.10.6
pydantic_core==2.27.2
pydub==0.25.1
pyface==8.0.0
Pygments==2.17.2
pyparsing==3.2.0
PyQt5==5.15.11
PyQt5-Qt5==5.15.2
PyQt5_sip==12.17.0
python-dateutil==2.8.2
python-json-logger==2.0.7
python-multipart==0.0.20
pytz==2024.1
pywin32==306
pywinpty==2.0.12
PyYAML==6.0.1
pyzmq==25.1.2
qtconsole==5.5.1
QtPy==2.4.1
referencing==0.32.0
regex==2024.11.6
requests==2.32.3
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
rich==13.9.4
rpds-py==0.15.2
ruff==0.9.6
safehttpx==0.1.6
safetensors==0.5.2
scikit-learn==1.6.1
scipy==1.15.1
seaborn==0.13.2
semantic-version==2.10.0
Send2Trash==1.8.2
shellingham==1.5.4
six==1.16.0
sniffio==1.3.0
soupsieve==2.5
stack-data==0.6.3
starlette==0.45.3
sympy==1.13.1
terminado==0.18.0
threadpoolctl==3.5.0
tinycss2==1.2.1
tokenizers==0.21.0
tomli==2.0.1
tomlkit==0.13.2
torch==2.6.0
torchvision==0.21.0
tornado==6.4
tqdm==4.67.1
traitlets==5.14.0
traits==7.0.2
traitsui==8.0.0
transformers==4.48.3
typer==0.15.1
types-python-dateutil==2.8.19.14
typing_extensions==4.12.2
tzdata==2025.1
uri-template==1.3.0
urllib3==2.1.0
uvicorn==0.34.0
wcwidth==0.2.12
webcolors==1.13
webencodings==0.5.1
websocket-client==1.7.0
websockets==14.2
widgetsnbextension==4.0.9
wordcloud==1.9.4
wslink==2.3.2
xxhash==3.5.0
yarl==1.18.3

2.pip uninstall -r requirements.txt -y

根据 requirements.txt 文件中的信息卸载所有库,其中,-r 选项用于指定从文件中读取要卸载的库列表,-y 选项表示自动确认卸载操作,无需手动逐个确认。

<think>好的,我现在需要解决用户在使用Pandas的str.contains方法时传入列表导致的TypeError问题。首先,我要回忆一下str.contains的用法。根据引用[2],用户可能在Series上使用了apply方法,但每个元素是字符串,而str.contains只能在Series级别使用,不能在单个字符串上调用,所以会报错。 用户的问题是传入列表导致TypeError,可能他们想同时匹配多个模式。比如,有一个字符串列,想检查每个元素是否包含列表中的任意一个子字符串。这时候直接传递列表给str.contains是不行的,因为该方法接受的是字符串或正则表达式,而不是列表。 解决方案可能包括将多个模式合并成一个正则表达式,用|分隔。例如,列表['apple', 'banana']可以变成正则表达式'apple|banana',然后用str.contains(regex=True)来匹配。此外,需要注意转义特殊字符,或者使用apply结合lambda函数逐个检查每个模式。 另外,引用[4]提到Series可以运用ndarray或字典的索引操作,可能需要利用向量化操作来提高效率,而不是循环。比如,使用正则表达式或者逐元素应用函数。 可能需要分步骤说明:首先将列表转换为正则表达式,然后使用str.contains,或者使用apply和any()来检查是否存在列表中的任一元素。同时,需要处理可能的特殊字符,比如在正则表达式中转义它们。 还要考虑性能问题,如果数据量大,正则表达式可能更高效,而apply方法可能较慢。根据引用[1],Pandas在有序索引时使用二分查找,可能类似的优化在这里不适用,但需要注意方法的选择对性能的影响。 最后,要给出代码示例,演示如何正确使用str.contains或替代方法,并解释每一步的作用,确保用户理解如何避免TypeError。</think>### 解决Pandas中`str.contains`传入列表导致的TypeError问题 在Pandas中,`str.contains()`方法用于检测字符串中是否包含指定模式的子字符串。若直接传入列表(例如`[pattern1, pattern2]`)作为参数,会触发`TypeError: unhashable type: 'list'`错误,因为该方法要求模式参数为字符串或正则表达式[^2][^4]。以下是具体解决方案: --- #### 方法一:将列表转换为正则表达式 将列表中的多个模式合并为一个正则表达式,通过`|`符号分隔不同模式,并使用`regex=True`参数启用正则匹配: ```python import pandas as pd # 示例数据 df = pd.DataFrame({"text": ["apple pie", "banana cake", "cherry tart"]}) patterns = ["apple", "banana"] # 将列表转换为正则表达式 combined_pattern = "|".join(patterns) # 结果为 "apple|banana" result = df["text"].str.contains(combined_pattern, regex=True) print(result) ``` 输出: ``` 0 True 1 True 2 False Name: text, dtype: bool ``` --- #### 方法二:使用`apply`逐行匹配 若需要更复杂的逻辑(如区分大小写、部分匹配等),可使用`apply`结合`any()`逐行检查是否包含列表中的任意一个模式: ```python # 使用apply + lambda表达式 result = df["text"].apply(lambda x: any(pattern in x for pattern in patterns)) print(result) ``` 输出: ``` 0 True 1 True 2 False Name: text, dtype: bool ``` --- #### 关键注意事项 1. **正则表达式转义** 若模式中包含特殊字符(如`*`, `+`, `$`),需用`re.escape()`转义: ```python import re patterns = ["apple$", "banana*"] safe_patterns = [re.escape(p) for p in patterns] combined_pattern = "|".join(safe_patterns) ``` 2. **性能优化** 对于大规模数据,正则表达式(方法一)通常比`apply`(方法二)更快,因为前者利用了Pandas的向量化操作[^4]。 --- #### 错误示例分析 直接传递列表会导致TypeError: ```python # 错误用法 df["text"].str.contains(["apple", "banana"]) # 报错: TypeError ``` 原因:`str.contains()`的`pat`参数要求为字符串类型,列表属于不可哈希类型(unhashable type),无法直接处理[^2]。 --- ### 相关问题 1. **如何同时匹配多个正则表达式并返回匹配的具体模式?** > 可通过`str.extract()`结合正则表达式分组实现。 2. **在Pandas中如何忽略大小写进行子字符串匹配?** > 使用`case=False`参数,例如:`str.contains("pattern", case=False)`[^4]。 3. **如何统计某列中包含列表中任一模式的次数?** > 对方法一或方法二的结果调用`sum()`,例如:`result.sum()`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值