Trouble with streamlite.write() pandas dataframe does not show all information - python

I'm using st.write() to display a dataframe with long strings in a column. When i display the dataframe with st.write(df) it does not show all info, here is an example:
Here is the code:
with st.container():
st.write('<style>div[data-baseweb="table"] .cell {white-space: pre-wrap;}</style>',
unsafe_allow_html=True)
st.dataframe(df_observaciones, width=1500, height=450)
I want to see all the info and include line breaks if necesary, any help will be good.

One easy option is to use st.table(df), shows all the info.
https://docs.streamlit.io/library/api-reference/data/st.table
st.table(df)

Related

Styles when importing pd.Dataframe to excel.openpyxl library

I need help.You need to add pd.DataFrame to excel with a certain style.The openpyxl library. When executing the code specified below, the data is added to the sheet.I usually find the answers to my questions on this site or YouTube, but this problem has put me at a dead end.Even after reading the pandas.io.formats.style.Styler.to_excel documentation, I didn't understand anything. This is my first question on this site.If I made a mistake with the design of the question, then let me know so that I can work on the errors.I need a specific cell format (font, size, borders, etc.).I poked around a little and found this option.How do I add more style to indexes and headers? This option makes the auto-height of the line fit the size of the text, maybe there is still an option for the auto-width of the column?
my result
the result I wantThanks!!
properties = {"border": "2px solid black", "font-size": "28px"}
with pd.ExcelWriter(f'{name}.xlsx', mode="a", if_sheet_exists='replace', engine="openpyxl") as writer:
vkd.style.set_properties(**properties).to_excel(writer, sheet_name="Остаток по датам коровы",startcol=1,startrow=1)

AgGrid in Python giving blank grid

AgGrid in Python giving blank grid when run with Justpy to display a Dataframe on the webpage.
Please find below the python code I am trying to run... It is giving a blank grid can you please help me debug???
import pandas as pd
import justpy as jp
w1=pd.DataFrame([[1,2,3],[2,3,4],[3,4,5]])
def grid_test():
print(w1)
wp = jp.WebPage()
jp.Strong(text=str(w1), a=wp)
grid = jp.AgGrid(a=wp)
grid.load_pandas_frame(w1)
return wp
jp.justpy(grid_test)
Does the example described in https://justpy.io/grids_tutorial/pandas/ work for you?
I encountered the same issue. The issue for me resolved when I made certain the recordset contained no null values. My orignal recordset had some null values.

Reading a dictionary from within a dictionary

I have a json file for tweet data. The data that I want to look at is the text of the tweet. For some reason, some of the tweets are too long to put into the normal text part of the dictionary.
It seems like there is a dictionary within another dictionary and I can't figure out how to access it very well.
Basically, what I want in the end is one column of a data frame that will have all of the text from each individual tweet. Here is a link to a small sample of the data that contains a problem tweet.
Here is the code I have so far:
import json
import pandas as pd
tweets = []
#This writes the json file so that I can work with it. This part works correctly.
with open("filelocation.txt") as source
for line in source:
if line.strip():
tweets.append(json.loads(line))
print(len(tweets)
df = pd.DataFrame.from_dict(tweets)
df.info()
When looking at the info you can see that there will be a column called extended_tweet that only encompasses one of the two sample tweets. Within this column, there seems to be another dictionary with one of those keys being full_text.
I want to add another column to the dataframe that just has this information along with the normal text column when the full_text is null.
My first thought was to try and read that specific column of the dataframe as a dictionary again using:
d = pd.DataFrame.from_dict(tweets['extended_tweet]['full_text])
But this doesn't work. I don't really understand why that doesn't work as that is how I read the data the first time.
My guess is that I can't look at the specific names because I am going back to the list and it would have to read all or none. The error it gives me says "KeyError: 'full_text' "
I also tried using the recommendation provided by this website. But this gave me a None value no matter what.
Thanks in advance!
I tried to do what #Dan D. suggested, however, this still gave me errors. But it gave me the idea to try this:
tweet[0]['extended_tweet']['full_text']
This works and gives me the value that I am looking for. But I need to run through the whole thing. So I tried this:
df['full'] = [tweet[i]['extended_tweet']['full_text'] for i in range(len(tweet))
This gives me "Key Error: 'extended_tweet' "
Does it seem like I am on the right track?
I would suggest to flatten out the dictionaries like this:
tweet = json.loads(line)
tweet['full_text'] = tweet['extended_tweet']['full_text']
tweets.append(tweet)
I don't know if the answer suggested earlier works. I never got that successfully. But I did figure out something else that works well for me.
What I really needed was a way to display the full text of a tweet. I first loaded the tweets from the json with what I posted above. Then I noticed that in the data file, there is something called truncated. If this value is true, the tweet is cut short and the full tweet is placed within the
tweet[i]['extended_tweet]['full_text]
In order to access it, I used this:
tweet_list = []
for i in range(len(tweets)):
if tweets[i]['truncated'] == 'True':
tweet_list.append(tweets[i]['extended_tweet']['full_text']
else:
tweet_list.append(tweets[i]['text']
Then I can work with the data using the whol text from each tweet.

i have developed the program and i am facing problems in it

Kindly give me the access to post quality questions. I am really upset by this action
You can try to pivot the table. Which may give the format you require.
Considering the information you gave as as ActionsOnly.csv
userId,movieId,rating
18,9,3
32,204,4
49,2817,1
62,160438,4
70,667,5
73,1599,1
73,4441,3
73,4614,3.5
73,86142,4
95,4636,2
103,71,1
118,3769,4
150,4866,2
You wan to find out what user rated what movie out of 5.
The userId is the index column, the movieId becomes the header row and the rating is what decides the values. If there is no Value it will display NaN or Not A Number
movie_pivot = movie.pivot_table(index='userId', columns='movieId', values='rating')
To Save a file in Pandas to CSV there is a simple command to_csv
so
movie_pivot.to_csv('ActionsOnly_pivot.csv')
Will save to csv.
So the full code you need is:
import pandas as pd
movie = pd.read_csv('movies.csv')
movie_pivot = movie.pivot_table(index='userId', columns='movieId', values='rating')
movie_pivot.to_csv('movies_pivot.csv')
I also strongly recommend reading about pandas, It is surprisingly easy and logical :)

Apply Number formatting to Pandas HTML CSS Styling

In Pandas, there is a new styler option for formatting CSS ( http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.core.style.Styler.html ).
Before, when I wanted to make my numbers into accounting/dollar terms, I would use something like below:
df = pd.DataFrame.from_dict({'10/01/2015': {'Issued': 200}}, orient='index')
html = df.to_html(formatters={'Issued': format_money})
format_money function:
def format_money(item):
return '${:,.0f}'.format(item)
Now I want to use the Style options, and keep my $ formatting. I'm not seeing any way to do this.
Style formatting for example would be something like this:
s = df.style.bar(color='#009900')
#df = df.applymap(config.format_money) -- Doesn't work
html = s.render()
This would add bars to my HTML table like so(Docs here: http://pandas.pydata.org/pandas-docs/stable/style.html):
So basically, how do I do something like add the bars, and keep or also add in the dollar formatting to the table? If I try to do it before, the Style bars don't work because now they can't tell that the data is numerical and it errors out. If I try to do it after, it cancels out the styling.
That hasn't been implemented yet (version 0.17.1) - but there is a pull request for that (https://github.com/pydata/pandas/pull/11667) and should come out in 0.18. For now you have to stick to using the formatters.

Categories