in Pandas, how can print() and display() render completely different numbers? - pandas

I have a dataframe, imported from a CSV. One of the columns is 1 or null (NaN).
When I view it with print, it shows as a 1.
When I view it with display it renders as a 0.0
When I evaluate it, with an operator like > it acts as a 1.
Why is this? How can I make sure display doesn't confuse me?

I can't reproduce the problem anymore, after running jupyter lab build after installing a new nbextension

Related

pyqt5 - error zsh: segmentation fault on Mac OS Monterey 12.4 [duplicate]

I am using PyQt based on Qt4. My Editor is PyCharm 2017.3 and my python version is 3.4. I am scraping some text from a website. I am trying to align that text to the center of the cell in a QTableWidget.
item = QTableWidgetItem(scraped_age).setTextAlignment(Qt.AlignHCenter)
self.tableWidget.setItem(x, 2,item)
Therefore while putting the item in the cell, I am trying to align it as per the documentation. The problem is that the data is not showing up.
It did show up when I removed setTextAlignment method as shown below
item = QTableWidgetItem(scraped_age)
self.tableWidget.setItem(x, 2,item)
This line of code:
item = QTableWidgetItem(scraped_age).setTextAlignment(Qt.AlignHCenter)
will not work properly, because it throws away the item it creates before assigning it to the variable. The variable will in fact be set to None, which is the return value of setTextAlignment(). Instead, you must do this:
item = QTableWidgetItem(scraped_age) # create the item
item.setTextAlignment(Qt.AlignHCenter) # change the alignment
This didn't work for me, and I'm not sure if it is because I'm using PyQt5 or it i did something wrong. I was trying to find something similar but for the whole table, and i finally stumbled upon something that worked and lets you center every cells or just one column at a time.
You have to use the delegate method:
#You're probably importing QtWidgets to work with the table
#but you'll also need QtCore for the delegate class
from PyQt5 import QtCore, QtWidgets
class AlignDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(AlignDelegate, self).initStyleOption(option, index)
option.displayAlignment = QtCore.Qt.AlignCenter
After implementing this in your code, you can add the following to your main window class or wherever the table is defined:
delegate = AlignDelegate(self.tableWidget)
self.tableWidget.setItemDelegateForColumn(2, delegate) #You can repeat this line or
#use a simple iteration / loop
#to align multiple columns
#If you want to do it for all columns:
#self.tableWidget.setItemDelegate(delegate)
Know this is an old question, but hope it can help someone else.
Bit late to the party but for those of you wondering how to do this on pyqt5
table = QTableWidgetItem() #QTWidgets.QTableWidgetItem() if importing QWidget from PyQt5
table.setTextAlignment(number)
setTextAlignment takes an int for the argument (alignment). Put the number in to get the result:
0:left
1:left
2:right
3:right
4:centre

How to export Pandas styled dataframe as an image to Databricks dbfs?

Context: I am writing a bot on Databricks using Python that will send to a Slack channel the image of a pandas dataframe table. That table was formatted using .style to make it faster for people to see the most important numbers.
I have two problems:
how can I save as an image a pandas dataframe that went through the .style method?
how can I open that image in another Databricks notebook?
Step 1 - OK: generating a sample dataframe.
import pandas as pd
my_df = pd.DataFrame({'fruits':['apple','banana'], 'count': [1,2]})
Step 2 - OK: then, I save a new variable in the following way to add to the table several formatting modifications that I need:
my_df_styled = (my_df.style
.set_properties(**{'text-align': 'center', 'padding': '15px'})
.hide_index()
.set_caption('My Table')
.set_table_styles([{'selector': 'caption',
'props': [('text-align', 'bottom'),
('padding', '10px')
]}])
)
Step 3 - Problem: trying to save the new variable as an image. But here, I am not being able to correctly do it. I tried to follow what was mentioned here, but they are using matplotlib to save it and it is something that I don't want to do, because I don't want to lose the formatting on my table.
my_df_styled.savefig('/dbfs/path/figure.png')
But I get the following error:
AttributeError: 'Styler' object has no attribute 'savefig'
Step 4 - Problem: opening the image in a different notebook. Not sure how to do this. I tried the following using another image:
opening_image = open('/dbfs/path/otherimage.png')
opening_image
But instead of getting the image, I get:
Out[#]: <_io.TextIOWrapper name='/dbfs/path/otherimage.png' mode='r'
encoding='UTF-8'>
For first question, savefig() is the method of Matplotlib so it is certainly not working if you try to do sth like df.savefig()
Rather, you should use another wrapper (matplotlib or other library in below link) to input the dataframe so that the dataframe can be converted into image with desired style.
https://stackoverflow.com/a/69250659/4407905
For the second question, I do not try Databrick myself, but I guessed it would be better if you do use to_csv(), to_excel(), to_json(), etc. method to export data into text-based format.

Jupyterlab Table dynamic output (sorting, filterung, ...)

Good evening everyone,
is there a way to dynamically display the output of a table (pandas dataframe) so that you can sort by a column in the output or filter a column?
I would have thought that this should be included in Jupyter by default, but I can't find a setting.
Maybe I just can't find such a setting, so I'm curious about your answers. :-)
There is an extension called qgrid.
Qgrid is a Jupyter notebook widget which uses SlickGrid to render pandas DataFrames within a Jupyter notebook. This allows you to explore your DataFrames with intuitive scrolling, sorting, and filtering controls, as well as edit your DataFrames by double clicking cells.
You can display your dataframe with widget, and sort, filter or even edit your data interactively.
import qgrid
qgrid_widget = qgrid.show_grid(dataframe, show_toolbar=True)
qgrid_widget
As of now (March, 2021), works with both jupyter lab: 3.0.10 and jupyter-notebook : 6.2.0
Example screenshot:

How to solve duplicate output by using search bar in ionic 4

I am using ionic 4. I want to do search function in my apps. I am using search bar and get the result from API.
My problem is it always output duplicate result.
One problem is I haven't typing the word finish it already match the result that I want to search, then it will output the result.
Another problem is when I finish typing it output again the result.
How can I solved or avoid this problem?
Here is my html code:
<ion-searchbar [(ngModel)]="name" (ionCancel)="onCancel($event)" (ionChange)="Search($event)"></ion-searchbar>
Here is my home.page.ts
Search() {
this.myService.getSearch(this.name);
}
The second one sounds like you are not wiping the data in your this.myService.getSearch call - check what it does in there, is it starting from a new list or just returning more results? If that doesn't solve that issue then post the code for that section.
The first issue is by design.
You can slow this down with the debounce option:
<ion-searchbar debounce="1500"></ion-searchbar>
There are two input events you can experiment with ionChange and ionInput - they act differently but I cannot remember exactly which does which, you will have to try them both. A third option would be to handle neither of them and just use a submit button.

View like function in Julia Juno

id like to start using Julia for computing instead of Python. But so far I miss very important tool - View-like function. There is View() function in R that displays whole dataframe. This is very handfull tool, I cant even imagime use Julia withouth this. Is just too early to use Julia?
So far I tried print(df).
I use Juno in the Atom IDE.
Print screen from R looks like this, I want to open my dataframe in another window, not i console.
Not entirely sure what you need because I've not used R, but maybe you're looking for the printtable() method.
printtable(df::AbstractDataFrame; header::Bool = true,
separator::Char = ',',
quotemark::Char = '"',
nastring::AbstractString = "NA")
please note, oddly, that it only accepts named arguments
hope this helps fam
Not available so far.
Although, as commented in the issue discussion, this is available for Jupyter notebooks (by calling stringmime("text/html", mydataframe) underneath).
What's currently missing is for Atom to pick up the generated table html. A quick search didn't throw any info on whether Atom can/is displaying tables.
EDIT:
Another alternative is using ElectronDisplay.jl, which does a pretty-display, although outside the Juno (or Atom) ecosystem:
using ElectronDisplay
using DataFrames
df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])
electrondisplay(df)
Tested with Julia 1.3: TableView: it allows browsing, sorting and filtering.
In Juno it display the table/DataFrame on the Plot Panel: