Python Pandas freezing why? - python-3.x

(I'm not very good in English but I will do my best)
Version of pandas = 1.3.4, python = 3.10.6 in all my env conda. And out of conda : python = 3.9.12 and pandas = 1.3.4
I'm working with Pandas since multiple years.
I often do this syntax in my script :
df[df['Column']=='Value']
But now it doesn't work at all and freeze all my scripts with no error at all in prompt.
I'm working on ubuntu 22 with Miniconda and i have multiple env.
The code i'm struggling with :
#!/usr/bin/env python
# coding: utf-8
import pandas as pd
df = pd.read_csv("data/corpus.csv")
print(df.shape)
df = df[df['Title']=='Yes']
print(df['Title'])
And this part of code work
var = df['Title']=='Yes'
But this wont:
df = df[var]
The print of df.shape is working but final print never show up, and prompt never print the end of interpreting the code.
I did try on different envs but that did not change the result of freezing my script.
The csv file is basic, nothing special, i did try with a new one created for testing but no change.
I did try in jupyter-notebook and in the terminal python prompt, no change.
I don't know what to do next, if you need more informations, i'm here to give it to you quickly.
Best regards,

The problem is coming from python=3.10.6. I downgrade to 3.8.16 and it's working well.
Thanks a lot Rainflow for your help.

Related

How to resolve 'No module named 'cPickle'' exception in virtualenv

I'm trying to run a program which connects to all databases(mysql,sqlite) and fetch data from it .
Python version - 3.6.8
Since the code is too long ,i'm showing only particular snippets.
def show_columns_mysql(cursor,tbname):
cursor.execute("""show columns from %s"""%(tbname))
rs=cursor.fetchall()
colname=[]
for i in rs:
colname.append(i[0])
return colname
There is no problem or issue if i exexute the program in normal python environment . When i try to execute this in virtual environment ,it shows me No module named 'cPickle' .
I have tried all the solutions but none solved my problem .
What was the problem ?
There is no cPickle in Python 3. Just import pickle. pickle will automatically use the C accelerator.
Install pickle. Then do:
import pickle as cPickle

How can I import code and markdown cells from Python to Jupyter notebook?

For version control reasons, I must save my code in .py files.
I would like to be able to able to import cells of Python code and Markdown documentation from .py files to Jupyter notebook.
For example, I would like to use Jupyter notebook to run my report code, which has multiple sections of code and documentation.
I am aware of the built-in %run and %load in Jupyter:
%run report.py
%load report.py
%run and %load run/load everything into one cell. I am looking for a solution which allows me to split a single python file to multiple notebook cells.
Thank you!
Have used 'p2j' to convert python file (.py) to ipython notebook file (.ipynb).
Try using the below steps:
pip install p2j
p2j your_python_file.py
For more details, refer https://github.com/raibosome/python2jupyter
Hope this helps.
I wrote an iPython extension which does this: ipython-cells.
It can be used like this:
$ pip install ipython-cells
>>> %load_ext ipython_cells
>>> %load_file test.py
>>> %cell_run 1
hello
>>> %cell_run 2
world
And the test file test.py
# In[1]
print('hello')
# In[2]
print('world')
It also supports cell range execution and Spyder cell delimiters. See the readme.
Try the built in
%load script.py
And do a little editing. Good If It has little markdown.
Learn the keyboard shortcut to split cell and convert to markdown.

Pandas.ExcelWriter not behave the same after pyinstaller to exe file

I am a absolute beginner of python and working on a small program to collect data and write multiple sheets to Excel using pandas, it worked as expected. However after convert to exe file using pyinstaller, it only write first sheet. No matter in onefolder or onefile mode.
I pasted the minimum code that can recreate the problem.
I know I can just writer.save() once in the end but in real program I writer multiple sheets in for loop and it's a lot more efficient and create much smaller xlsx file than just save once in the end.
PyInstaller: 3.4
Python: 3.7.2
pandas: 0.24.2
Platform: Windows-10-10.0.17763-SP0
I search the problem everywhere, tried to use hiddenimports to add different modules for pyinstaller, like such:
hiddenimports=['pandas', 'pandas.io.excel', 'numpy', 'pandas.compat', 'xlsxwriter', 'openpyxl'],
None of them works.
import pandas as pd
writer = pd.ExcelWriter('xlstest.xlsx')
df1 = pd.DataFrame(data=[[1, 2, 3]], columns=("a", "b", "c"))
df2 = pd.DataFrame(data=[[5, 6, 7]], columns=("d", "e", "f"))
df1.to_excel(writer, sheet_name="sheet1", index=False)
writer.save()
df2.to_excel(writer, sheet_name="sheet2", index=False)
writer.save()
I solved my own problem. I was using Pycharm for the project. It appears that pycharm using a virtual environment while pyinstaller using the local system environment. So I tried to compare the installed packages in both environment and find out what could be the reason. I have same version of pandas and openpyxl installed in both environment, which puzzled me for a while. Tried a lot of things and eventually find out it was XlsxWriter package caused the problem. I have XlsxWriter 1.1.5 installed in local environment but not in pycharm venv. As soon as I pip uninstalled it from local env. My compiled exe file works correctly.
I am still looking into how to correctly using pyinstaller in pycharm venv. Any comment on that is welcomed.

Get a table from a website to a CSV file

I need to transform the tables from a website to a CSV file.
I am using Python 3 from a raspberryPi.
Here is the code that I am using:
from urllib.request import urlopen, Request, URLError
from TableParser import TableParser
url_addr ='http://www.tbs-sct.gc.ca/pses-saff/2017-2/results-resultats/bq-pq/12/org-eng.aspx#s1'
req = Request(url_addr)
url = urlopen(req)
tp = TableParser()
tp.feed(url.read())
#Here I want the first table
my_table = tp.get_tables()[0]
filename = 'table_as_csv.csv'
f = open(filename, 'wb')
with f:
writer = csv.writer(f)
for row in table:
writer.writerow(row)
When I try to run it, I get:
ImportError: No module named 'TableParser'.
I am new to Python and I don't know what to do. Any help would be very appreciated.
Thanks!
First off, I think you should familiarise yourself with python and its syntax as well as how it handles errors. Clearly you want to do something quick and be done with it. Here's a simple python tutorial
To answer your question
ImportError exceptions occur when you do not have that particular module installed using pip installation preferrably. Until it is installed your script wont run successfully.
The error means TableParser has not been installed yet and you could do this by
pip3 install TableParser
This assumes you know where the module is and can be installed using pip
I see you are using the module csv but have not imported it as well. You should.

can't import http in python 3.5.1 but it works in cmd

I'm still learning python. I have windows 8 and had downloaded 2.7 as well as 3.5 because two modules I used in the past respectively used different python versions. Now though, I'm trying to run a script where the first line is import http.client or http. Neither of these work though. In cmd, python returns 3.5.1 currently and import http.client and import http return with no errors but in my IDE these don't work. Why not?
You can follow the Pycharm documentation here to change the Python version for your project from Python 2 to Python 3.
(In particular, Selecting Python interpreter for a project section)

Resources