Dataframe head not shown in PyCharm - python

I have the following code in PyCharm
import pandas as pd
import numpy as np
import matplotlib as plt
df = pd.read_csv("c:/temp/datafile.txt", sep='\t')
df.head(10)
I get the following output:
Process finished with exit code 0
I am supposed to get the first ten rows of my datafile, but these do not appear in PyCharm.
I checked the Project interpreter and all settings seem to be alright there. The right packages are installed (numpy, pandas, matplotlib) under the right Python version.
What am I doing wrong? Thanks.

PyCharm is not Python Shell which automatically prints all results.
In PyCharm you have to use print() to display anything.
print(df.head(10))
The same is when you run script in other IDE or editor or directly python script.py

For printing all data
print(df)
By Default it will print top 5 records for head.
print(df.head())
If you need 10 rows then you can write this way
print(df.head(10))

I did File-Invalidate Caches/Restart Option Invalidate and after that I was able to get the head:

Related

How can I configure a line chart using pivot_ui?

I would like to create a dynamic line chart in Voila. How can I manipulate the below code to show a standard line graph where the x axis equals column "a" and the y axis equals column "b"? Potentially the user can then dynamically update the output to make the y axis equal to column "c" by drag and drop etc.
from pivottablejs import pivot_ui
import pandas as pd
import IPython
df = pd.DataFrame(("a": [1,2,3], "b": [30,45,60],"c": [100,222,3444]))
display.display(df)
pivot_ui(df,outfile_path='pivottablejs.html',
rendererName="Line Chart",
cols= ["b","c"]
rows= ["a"],
aggregatorName="Sum"
)
display.display(IPython.display.HTML('pivottablejs.html"))
Thank you.
This should be a comment but I cannot post code with comments easily.
Please always test your code, preferably in a new notebook so it is fresh kerenl, before you post it.
Your dataframe assignment won't work. Should it be something like below?
That display code won't work in Jupyter notebook classic or JupyterLab presently.
Try something like this for assignment and display:
import pandas as pd
df = pd.DataFrame({"a": [1,2,3], "b": [30,45,60],"c": [100,222,3444]})
display(df)
That works in the classic notebook, JupyterLab, and Voila to make & display the dataframe.
Related to this is that is advisable to develop for Voila in JupyterLab. JupyterLab's rendering machinery is more modern and so closer to what Voila uses.
You can easily test renderings in launches from the Voila binder example page. Go there and click 'launch binder for the appropriate rendering. From JupyterLab you can select the Voila icon from the toolbar just above an open notebook and get the Voila rendering on the side.

How to plot with (raw) pandas and without Jupyter notebook [duplicate]

This question already has answers here:
Saving plots (AxesSubPlot) generated from python pandas with matplotlib's savefig
(6 answers)
Pandas plotting in Windows terminal
(2 answers)
Pandas plot doesn't show
(4 answers)
Closed 7 months ago.
I am aware that pandas offer the opportunity to visualize data with plots. Most of the examples I can find and even pandas docu itself use Jupyter Notebook examples.
This code doesn't work in a row python shell.
#!/usr/bin/env python3
import pandas as pd
df = pd.DataFrame({'A': range(100)})
obj = df.hist(column='A')
# array([[<AxesSubplot:title={'center':'A'}>]], dtype=object)
How can I "show" that?
This scripts runs not in an IDE. It runs in a Python 3.9.10 shell interpreter in Windows "Dos-Box" on Windows 10.
Installing jupyter or transfering the data to an external service is not an option in my case.
Demonstrating a solution building on code provided by OP:
Save this as a script named save_test.py in your working directory:
import pandas as pd
df = pd.DataFrame({'A': range(100)})
the_plot_array = df.hist(column='A')
fig = the_plot_array [0][0].get_figure()
fig.savefig("output.png")
Run that script on command line using python save_test.py.
You should see it create a file called output.png in your working directory. Open the generated image with your favorite image file viewer on your machine. If you are doing this remote, download the image file and view on your local machine.
You should also be able to run those lines in succession in a interpreter if the OP prefers.
Explanation:
Solution provided based on the fact Pandas plotting uses matplotlib as the default plotting backend (which can be changed), so you can use Matplotlib's ability to save generated plots as images, combined with Wael Ben Zid El Guebsi's answer to 'Saving plots (AxesSubPlot) generated from python pandas with matplotlib's savefig' and using type() to drill down to see that pandas histogram is returned as an numpy array of arrays. (The first item in the inner array is an matplotlib.axes._subplots.AxesSubplot object, that the_plot_array [0][0] gets. The get_figure() method gets the plot from that matplotlib.axes._subplots.AxesSubplot object.)
Try something like this
df = pd.DataFrame({'A': list(range(100))})
df.plot(kind='line')

Does a technical solution exist to open a .mpr file in python?

I have to read informations from a .mpr file (in order to complete a dataset). Does anyone know how it works ?
I tried with pandas, open(), but on the net i got anything ..
Thanks a lot !
There's a package on GitHub called galvani that you can use. Install from source (it seems that their pip install galvani is not updated)
Then simply do:
from galvani import BioLogic as BL
import pandas as pd
mpr = BL.MPRfile('path_to_your.mpr')
df = pd.DataFrame(mpr.data)
df.head()
You will see your data

pandas.read_csv cant find my path error

So I tried to run this code.
import pandas
i = input("hi input a csv file..")
df = pandas.read_csv(i)
and I got an error saying
FileNotFoundError: File b'"C:\\Users\\thomas.swenson\\Downloads\\hi.csv"' does not exist
but then if I hard code that path that 'doesn't exist' into my program it works fine.
import pandas
df = pandas.read_csv("C:\\Users\\thomas.swenson\\Downloads\\hi.csv")
it works just fine.
Anyone know why this may be happening?
I'm running python 3.6 and using a virtualenv
looks like the input function was placing another set of quotes around the input.
so ill just have to remove them and it works fine.

pandas read_csv working only as root user

I am reading a csv file using pandas. It works fine if I run script as root user. But when I try to run it with different user it does not read data and gives:
error : KeyError: 'no item named 0'
it appears at:
dt = pd.read_csv('rt.csv', header=None).fillna('').set_index(0).to_dict()[1]
Btw, I am working on Ubuntu 12.02 and using anaconda, which is installed in root user and other user as well (which is giving error)
Please help.
You like have different pandas versions installed as user and root.
I get the same error with version 0.16.2 when I use the wrong delimiter.
Have a look at your data in rt.csv.
For example, this would work for a whitespace-delimited file:
dt = pd.read_csv('rt.csv', header=None,
delim_whitespace=True).fillna('').set_index(0).to_dict()[1]
Check the file and adapt the delimiter accordingly.

Categories

Resources