How to add data in newly added row in ipysheet? - python

When I click on add row button Iam able to add new row but how to add data to that new row, Iam using data frames here ?
Please find the executable code
import qgrid,ipysheet
from ipywidgets import widgets
import numpy as np
import pandas as pd
#qgrid.show_grid(df,show_toolbar=True)
random_data = [['ABC','DEF','GHI'],
[0.69494459,0.90502388,0.13374771],
[0.89609645,0.66166011,0.12123602],
[0.51229257,0.57240808,0.25482961],
[0.25478973,0.08438657,0.76977642]]
df = pd.DataFrame(random_data, columns=["Col1", "Col2", "Col3"])
sheet = ipysheet.from_dataframe(df)
btn = wg.Button(description = 'Add Row')
def on_bttn_clicked(b):
sheet.rows += 1
btn.on_click(on_bttn_clicked)
display(btn,sheet)

Related

Pandas table customization

This is a section of my python code, this class retrieves a sheet from smartsheet and turns it into a df, which later uses to display on a table on my GUI. I've been trying to change row colors but cant seem to get it right, in the code I managed to change the color of columns, however when I pass self.table.rowcolors... it does nothing. Anyone has an idea of what else I could try?
class SmartsheetFrame(Frame):
def __init__(self, master):
super().__init__(master)
self.configure(bg='white')
# Get the sheet
sheet_id = 3839061875025796
ss_client = smartsheet.Smartsheet(access_token=os.environ.get('SMARTSHEET_ACCESS_TOKEN'))
sheet = ss_client.Sheets.get_sheet(sheet_id)
rows = sheet.rows
columns = sheet.columns
#Creating a dataframe from the sheet
df = pd.DataFrame(columns=[col.title for col in columns])
for row in rows:
df.loc[row.id] = [col.value for col in row.cells]
last_4_rows = df.tail(4)
# Create a Table widget and set its properties
self.table = pt.Table(self, dataframe=df, showstatusbar=True, height=600, width=1200)
# Specify the row index you want to color
self.table.columncolors['AEM ID'] = 'forestgreen'
self.table.columncolors['Intel ID'] = 'midnightblue'
self.table.columncolors['Site'] = 'darkslategrey'
self.table.columncolors['Mode'] = 'firebrick'
self.table.redraw()
self.table.show()
I've tried many methods that I found online, so far I've managed to chage the color of columns, but havent been able to do the same for rows

How to append DataFrame data when using Streamlit

Streamlit with Dataframe add row into the table action and also download as csv by Button with still showing the dataframe on screen.
OP1: Using session_state (Best way to manage and applies for more)
import pandas as pd
import streamlit as st
# "st.session_state object:", st.session_state
if "df_result" not in st.session_state:
st.session_state['df_result'] = pd.DataFrame(columns=['h1','h2'])
# st.write(st.session_state)
def onAddRow():
data = {
'h1':"something",
'h2':"something",
}
st.session_state['df_result'] = st.session_state['df_result'].append(data, ignore_index=True)
st.button("Add row", on_click = onAddRow)
#st.cache
def convert_df(df):
return df.to_csv().encode('utf-8')
st.download_button(
"Press to Download",
convert_df(st.session_state.df_result),
"file.csv",
"text/csv",
key='download-csv'
)
st.dataframe(st.session_state['df_result'])
OP2: Basic append on global parameters but it will reset view when take action download btn because it rerun.
import pandas as pd
import streamlit as st
df_log = pd.DataFrame(columns=['h1','h2'])
df_log_process = st.dataframe(df_log)
for x in range(10):
df_log = df_log.append(
{
'h1':"something",
'h2':"something",
}, ignore_index=True)
df_log_process = df_log_process.dataframe(df_log)

List of values not showing up in sg.Combo drop down list in PySimpleGUI?

My code below does not show the list specified (based on parsing excel file dataframe columns) and the drop down list is empty.I am unsure of what I am missing here, please could someone help out? Thanks
Here is a example:
import PySimpleGUI as sg
import pandas as pd
from os import listdir
header_list=[]
layout = [ [sg.Text('Browse to file:'), sg.Input(size=(20,1), key='input'),sg.FileBrowse (key='filebrowse'),
sg.Button('Accept')],
[sg.Text('Choose a header')],
[sg.Combo(header_list, size=(20,1),enable_events= True, key='DROP')],
[sg.Cancel()]]
window = sg.Window('Choose the col', layout)
event, values = window.Read()
while True:
event, values = window.Read()
if event is None or event == 'Exit':
break
if event == 'Accept':
inputfile = values['filebrowse']
df = pd.read_excel(inputfile, header=None)
#print(df)
data = df.values.tolist()
header_list = df.iloc[0].tolist()
#print(header_list2)
data = df[1:].values.tolist()
window['DROP'].update([header_list][0])
window.Close()
I got this to work after adding enable_events=True in the sg.Button
layout = [ [sg.Text('Browse to file:'), sg.Input(size=(20,1), key='input'),sg.FileBrowse (key='filebrowse'),
sg.Button('Accept', enable_events=True)],
and adding values=[header_list][0] to the update method
window['DROP'].update(values=[header_list][0])

How to use data frame in python web application code

I am trying to create a python web application to show a time series graph with gold price distribution. I have added app layout, app calls back, and update_graph function as well. after that, I tried to take a copy of my data frame and save it to a new data frame as 'dff' but it is throwing an error. Also at the end of the code, I have put 'return fig' and it is throwing an error as well. I am fairly new to python and need help to figure out what's wrong with my code. Below is the entire code.
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input,Output
from urllib.request import urlopen, Request
url = "http://goldpricez.com/gold/history/lkr/years-3"
req = Request(url=url)
html = urlopen(req).read()
df = pd.read_html(url) # this will give you a list of dataframes from html
df1 = df[3]
first_val = df1.iloc[0][0]
date = df1[0]
price = df1[1]
data = [df1[0],df1[1]]
headers = ["Date", "Price"]
df3 = pd.concat(data, axis=1, keys=headers)
from datetime import datetime
df3['Date'] = df3['Date'].apply(lambda x: datetime.strptime(x, '%d-%m-%Y'))
df3['Year'] = pd.DatetimeIndex(df3['Date']).year
df3['Month'] = pd.DatetimeIndex(df3['Date']).month
df3['Day'] = pd.DatetimeIndex(df3['Date']).day
df3['WDay'] = df3['Date'].dt.dayofweek
df3['WeekDayName'] = pd.DatetimeIndex(df3['Date']).day_name()
print(df3['WDay'])
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df3.to_excel(writer, sheet_name='Sheet1')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
app = dash.Dash(__name__)
app.layout = html.Div([
html.H1("Gold Price Analyst" , style={'text-align' : 'center'}),
dcc.Graph(id='this_year_graph', figure={})
])
#app.callback(
[Output(component_id='this_year_graph', component_property='figure')]
)
def update_graph():
dff = df3.copy()
dff = [df4['Date'],df4['Price']]
fig = px.line(dff , x=dff['Date'], y=dff['Price'])
return fig
if __name__ =='__main__' :
app.run_server(debug=True)
def update_graph():
dff = df3.copy()
dff = [df4['Date'],df4['Price']]
fig = px.line(dff , x=dff['Date'], y=dff['Price'])
return fig

PySpark/HIVE: append to an existing table

Really basic question pyspark/hive question:
How do I append to an existing table? My attempt is below
from pyspark import SparkContext, SparkConf
from pyspark.sql import HiveContext
conf_init = SparkConf().setAppName('pyspark2')
sc = SparkContext(conf = conf_init)
hive_cxt = HiveContext(sc)
import pandas as pd
df = pd.DataFrame({'a':[0,0], 'b':[0,0]})
sdf = hive_cxt.createDataFrame(df)
sdf.write.mode('overwrite').saveAsTable('database.table') #this line works
df = pd.DataFrame({'a':[1,1,1], 'b':[2,2,2]})
sdf = hive_cxt.createDataFrame(df)
sdf.write.mode('append').saveAsTable('database.table') #this line does not work
#sdf.write.insertInto('database.table',overwrite = False) #this line does not work
Thanks!
Sam
It seems using option('overwrite') was causing the problem; it drops the table and then recreates a new one. If I do the following, everything works fine:
from pyspark import SparkContext, SparkConf
from pyspark.sql import HiveContext
conf_init = SparkConf().setAppName('pyspark2')
sc = SparkContext(conf = conf_init)
print(sc.version)
hive_cxt = HiveContext(sc)
hive_cxt.sql('USE database')
query = """
CREATE TABLE IF NOT EXISTS table (a int, b int)
STORED AS parquet
"""
hive_cxt.sql(query)
import pandas as pd
df = pd.DataFrame({'a':[0,0], 'b':[0,0]})
sdf = hive_cxt.createDataFrame(df)
sdf.write.mode('append').format('hive').saveAsTable('table')
query = """
SELECT *
FROM table
"""
df = hive_cxt.sql(query)
df = df.toPandas()
print(df) # successfully pull the data in table
df = pd.DataFrame({'a':[1,1,1], 'b':[2,2,2]})
sdf = hive_cxt.createDataFrame(df)
sdf.write.mode('append').format('hive').saveAsTable('table')
I think previously you forgot use the format option which caused the issue for you when you are trying to append and not overwrite like you mentioned above.

Categories