uploading a csv file in pandas python in pycharm - python

I want to upload a csv file in pandas.
Here is the code :
import pandas as pd
df = pd.read_csv('iris.csv')
print(df)
I tried it with different files, too.
The csv file and the py file are located in the same location,
but every time this error pups up:
AttributeError: partially initialized module 'pandas' has no attribute 'read_csv' (most likely due to a circular import)
I tried the file location in read_cvs() too, but nothing changed.
and i tried uninstalling and reinstalling pandas
and also tried with putting csv file in all files like venv
but nothing changed
i tried the code and the csv file in visual studio code and it worked,
but i want it in pycharm

In your error, it shows that the error is coming from circular imports. Uninstall pandas using pip uninstall pandas and then install it with pip install pandas. Furthermore, read this article on circular imports and how to deal with them.

Related

Error when executing python script in PowerBI

Error
Details: "ADO.NET: Python script error.
<pi>Traceback (most recent call last):
File "PythonScriptWrapper.PY", line 2, in <module>
import os, pandas, matplotlib
ModuleNotFoundError: No module named 'pandas'
</pi>"
I have imported data into PowerBi, I am now trying to execute some python script on the imported data, but I get the above error.
Please note that I have installed both numpy and pandas through the pip install function in cmd.
Also note that I am not using anaconda, I have installed python directly on my machine (most of the threads I've looked at reference solutions that are to do with Anaconda).
Thanks
Install python latest version using https://www.python.org/downloads/
In cmd, verify python installation using command ‘python –version’
Then install os,pandas,matplotlib,numpy using pip install.
import and load the required dataset in Powerbi Desktop. We can also create visualization graphs and plots using python script. in visualizations tab, select python visual(PY) and select required fields from the dataset. a python script editor will be displayed.
For example,
We used Iris dataset
The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:
dataset = pandas.DataFrame(Species, Id, PetalLengthCm, PetalWidthCm, SepalLengthCm, SepalWidthCm)
dataset = dataset.drop_duplicates()
import matplotlib.pyplot as plt
ax=plt.gca()
dataset.plot(kind='line',x='Species',y='SepalLengthCm',color='blue',ax=ax)
plt.show()
output

library installed in site-packages but not in lib folder

I was trying to use urllib library but an error was shown
import urllib.request
Traceback (most recent call last):
File "E:\coding\python for everybody\third part\urllib.py", line 1, in <module>
import urllib.request
File "E:\coding\python for everybody\third part\urllib.py", line 1, in <module>
import urllib.request
ModuleNotFoundError: No module named 'urllib.request'; 'urllib' is not a package
process is terminated with return code 1.
I installed urllib3 using pip but, the package was installed in roaming/python/python39/site-packages.
While the path is set to C:\Program Files\Python\scripts. The folders python39 and python are located in different folders. Also, upon checking I found that package urllib was installed already C:\Program Files\Python\scripts.
I don't know how python is accessing the packages and how it determines the location as none of the packages are being imported (except random and other preinstalled packages).This problem has been very problematic not only this time but many times before.
PS: some time ago I deleted python (6 months approx) and at that time python was installed in roaming is this the result of that?
I am also using anaconda distribution, but the above code was being written in sublime text(Windows 10).
UPDATE: ISSUE WAS SOLVED BY CHANGING THE FILE NAME(LOOK IN THE COMMENTS)
If you are using code editors like VSCode, Notepad+++ and not PyCharm or other python IDE. I think you should check your path and make sure that you have same python version.
Your filename is the same as the library that causes confusion, please change the name as python will get looking at the working directory for import hence the error

AttributeError using Pandas 0.22: pandas._libs.lib has no attribute 'isscalar'

I'm trying to use pandas 0.22, due mainly to 0.23.x's problems with pd.to_sql as specified here, on Windows 7. When I run
import pandas as pd
I get
AttributeError: module 'pandas._libs.lib' has no attribute 'isscalar'
From
inference.py, line 20
In fact, when I upgrade pandas to 0.23.4 (the latest version at the time of writing) I get the same problem, one which didn't exist before I tried to downgrade.
If I edit pandas.cor.dtypes.inference and set is_scalar to be True, I get another error:
ImportError: cannot import name 'Timestamp'
I haven't found anywhere else online which seems to solve this.
Thanks!
SOLVED
I navigated to the pandas location in my hard drive and removed the folder manually. I imagine that my installation had been messed up and it wasn't reinstalling properly. Just deleted, and reinstalled from cmd.

AttributeError: module 'pandas' has no attribute '__version__'

Please note, this is not a duplicate question. I'm aware of how to find versions of libraries in python. What's caught my goat is the apparent anomaly when the __version__ command is called in two different ways.
Has anyone faced this before? When I type the following in IDLE, I get the output which specifies the pandas version.
>>>import pandas as pd
>>>print(pd.__version__)
0.22.0
When I put the same two lines of code into a program, save it and run it, I get the following error:
AttributeError: module 'pandas' has no attribute '__version__'
It does not make any sense to me. Appreciate any thoughts on this please.
It sounds like you're not importing the real pandas module when you run your script from a file. When I run your script in a file on my machine it prints the pandas version correctly.
Maybe you have a file called pandas.py or a directory called pandas in your current working directory when you run the script? Or maybe there is a pandas.py or a pandas directory in a directory in your PYTHONPATH environment variable? (Other than the real pandas, that is.) Try running this program and see if the output looks like the proper pandas source file, or something else.
import pandas as pd
print(pd.__file__)

AttributeError: module 'pandas' has no attribute 'read_csv' Python3.5

I have been successfully using pandas.read_csv since long but suddenly it starts giving the error while I try to read a csv file
df = pd.read_csv('file.csv', encoding='utf-8')
The error is
AttributeError: module 'pandas' has no attribute 'read_csv'
I have tried to upgrade pandas but does not work. I tried to search and got this answer but when I search csv.py file in my pandas I didn't find any. So i tried to hover over the pandas.read_csv method which takes me to parsers.py file. But in that file there is no specific method named read_csv but it directed to another parser funtion like this
# parser.py (built-in file in pandas) file has this implementation
read_csv = _make_parser_function('read_csv', sep=',')
read_csv = Appender(_read_csv_doc)(read_csv)
I don't understand how should it start working again ? Any suggestions
I had the same problem when trying to run the following code in Jupyter/ipython.
import pandas as pd
df = pd.read_csv("weather_data.csv")
df
I realized I had a file named pandas.py. In fact, had two others named pandas1.py and pandas2.py as well. I changed them all and then it worked perfectly:) Lesson learned.
So I am writing an answer myself. I just noticed that I created a file random.py in my project which was creating a conflict with random.py in pandas package. Renaming my current file to something else worked for me :)
I faced the same problem and the solution that worked for me is as below.
Initially I installed the pandas and numpy with a regular user account. It installed library but there were few conflicts. So I uninstalled the libraries using pip uninstall package then installed them back as sudo account using sudo -H pip install package.
I hope it helps other people facing similar issue.
You literally just need to make sure that you have no ".py" files that have names of the same names of the packages. Like pandas.py, numpy.py etc..
Try print(pd)
Make sure you are getting this kind of output
<module 'pandas' from 'C:\\Users\\adarsh\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\pandas\\__init__.py'>
Otherwise, there may be another python file named pandas in your current working directory
For more click here
I also got the same problem but when I made a separate directory for the python file problem was solved. Make sure there is no other python file in your directory.
It is either the file named pandas.py or csv.py in the same directory or a name similar to one of the pandas files(which are many) in the same directory. I had a file named tokenize.py which it did not like. You can try checking in the pandas directory (search results in pandas) whether there are similar file names.
your project contains pandas.py possibly. rename it and try again.. would work.
Put your csv file in the directory or folder where your python project files are . This solution worked for me.
Make sure that, the library imported and the filename you are executing or the file you are having in the directory wont be having the samename.
Explainer Screenshot
Try to rename the Filename as Pandas1 or Pandas_Tutorial.py. It will solve the Problem.

Categories