I would like to start the graph from the first non-zero or non NaN value, also if possible, only connect non-zero/ non NaN terms.
def CreateAvgGraph(input_data):
KK = test3.loc[[input_data],:]
K = KK.T
K = K.fillna(0)
K = K.reset_index()
list1a = K['index'].tolist()
list2a = K[input_data].tolist()
return dcc.Graph(
id='example-graph2',
figure={
'data': [
{'x' : list1a , 'y': list2a, 'type':'line','name' :input_data},
],
'layout': {
'title': str(input_data) + ' Average Price'
}
}
)
[![enter image description here][1]][1]
Removing the fillNa doesn't really help as the view scale is too much.
def CreateAvgGraph(input_data):
KK = test3.loc[[input_data],:]
K = KK.T
K = K.reset_index()
list1a = K['index'].tolist()
list2a = K[input_data].tolist()
return dcc.Graph(
id='example-graph2',
figure={
'data': [
{'x' : list1a , 'y': list2a, 'type':'line','name' :input_data},
],
'layout': {
'title': str(input_data) + ' Average Price'
}
}
)
I have managed to do an ugly fix, but there has to be a better way?
def CreateAvgGraph(input_data):
KK = test3.loc[[input_data],:]
K = KK.T
K = K.fillna(0)
K = K.reset_index()
list1a = K['index'].tolist()
list2a = K[input_data].tolist()
list2aa = []
list1aa =[]
for i in range(0,len(list1a)):
if list2a[i] > 0:
list1aa.append(list1a[i])
list2aa.append(list2a[i])
else:
continue
return dcc.Graph(
id='example-graph2',
figure={
'data': [
{'x' : list1aa , 'y': list2aa, 'type':'line','name' :input_data},
],
'layout': {
'title': str(input_data) + ' Average Price'
If you simply want to plot all non-nan value, you should just drop the nan values rather than filling them with zeros, i.e. you should replace K.fillna(0) with K.dropna().
Related
I'm currently working on a dash_table in which I have already incorporated an add row button as well as delete. I would like the user to also be able to add rows by using a slider value. How would I be able to do that with making changes to current code?
html.Div(children=[
html.Label('# of Wells on Pad', style={'color':colors['text']}),
dcc.Slider(
id='well_slider',
min=0,
max=12,
step=1,
marks={i: f' {i}' if i == 1 else str(i) for i in range(13)},
value=0,
),
], style={'padding': 10, 'flex': 1, 'background-color':colors['background'],'margin':20})
], style={'display': 'flex', 'flex-direction': 'row',}),
# html.
html.Div(children=[
dash_table.DataTable(
id='projection_table',
columns=[{
'name': i,
'id': i,
} for i in table_outputs],
style_cell={'text-align':'center'},
data=[
{'column-{}'.format(i): (j + (i-1)*5) for i in range(1, 5)}
for j in range(5)
],
editable=True,
fill_width=True,
row_deletable=True,
export_format='xlsx',
export_headers='display',
style_table={'overflowX':'scroll'}
),
html.Button('Add Row', id='editing-rows-button', n_clicks=0, style={'margin':5}),
html.Div(id='testingSlider', style={"color":colors['text']})
],style={'margin':10})
#app.callback(
Output('projection_table', 'data'),
Input('editing-rows-button', 'n_clicks'),
State('projection_table', 'data'),
State('projection_table', 'columns')
)
def add_row(n_clicks, rows, table_outputs):
if n_clicks > 0:
rows.append({c['id']: '' for c in table_outputs})
return rows
#app.callback(
Output('testingSlider', 'children'),
Input('well_slider', 'value'),
State('projection_table', 'data'),
State('projection_table', 'columns')
)
def add_row_slider(value, rows, table_outputs):
if value > 0:
rows.append({c['id']: '' for c in table_outputs})
return rows
I am trying to add a column of one-word categories from analyzing a column that contains a sentence in each row
I tried the following code but it kept giving me errors!
def loan_cat(row):
rows = df[df.columns[0]].count()
for i in rows:
data = df['purpose'][i]
if 'house' in data:
return 'house'
elif 'education' | 'university' in data:
return 'education'
elif 'wedding' in data:
return 'wedding'
elif 'car' in data:
return 'car'
elif 'real' in data:
return 'real estate'
elif 'property'in data:
return 'property'
return 'undefined'
df['purpose_1'] = df.apply(loan_cat, axis=1)
is there a better way to analyze and categorize the data?
Use a dict
import pandas
data = pandas.Series(["purchase a house",
"purchase car",
"supplemental education",
"burger",
"attend university"])
arr = {"house": "house",
"education": "education",
"university": "education",
"car": "car"}
def foo(s, d):
for k, v in d.items():
if k in s:
return v
return "NA"
data.apply(lambda x: foo(x, arr))
# 0 house
# 1 car
# 2 education
# 3 NA
# 4 education
# dtype: object
I figured out the answer:
def loan_cat(value):
if 'hous' in value:
return 'House'
elif 'educ' in value:
return 'Education'
elif 'university' in value:
return 'Education'
elif 'wedding' in value:
return 'Wedding'
elif 'car' in value:
return 'Car'
elif 'real' in value:
return 'Real Estate'
elif 'property'in value:
return 'Property'
return 'undefined'
df['purpose_cat'] = df['purpose'].apply(lambda value: loan_cat(value))
print(df['purpose_cat'].value_counts())
Please how to add a new element to an array which is a value of a dictionary?
When I try to add the element like this:
res[key].append(newelement)
it just erases the previous value.
My complete code looks like
data = [{"signal_name": "X", "signal_value": "valueX1"},
{"signal_name": "Y", "signal_value": "valueY1"},
{"signal_name": "Z", "signal_value": "valueZ1"},
{"signal_name": "X", "signal_value": "valueX2"}]
res = {}
for i in data:
for k, v in i.items():
if k == "signal_name":
res[v] = []
temp = v
if k == "signal_value":
res[temp].append(v)
my output looks like this
Reading from input
{'X': ['valueX1']}
{'X': ['valueX1'], 'Y': ['valueY1']}
{'X': ['valueX1'], 'Y': ['valueY1'], 'Z': ['valueZ1']}
{'X': ['valueX2'], 'Y': ['valueY1'], 'Z': ['valueZ1']}
Done reading
so the X value is updated rather than contains the ['valueX1', 'valueX2']
res[temp].append(v) works well. In res[v] = [] line, you are assigning an empty list when you encounter X second time and ever time you encounter again.
I recommend you to use dictionary get() function.
res = {}
for d in data:
key = d["signal_name"]
value = d["signal_value"]
l = res.get(key, []) # Return [] if key is not in the dictonary
l.append(value)
res[key] = l
print(res)
Output:
{'X': ['valueX1', 'valueX2'], 'Y': ['valueY1'], 'Z': ['valueZ1']}
Try the below code, Hope this will help:
data = [{"signal_name": "X", "signal_value": "valueX1"},
{"signal_name": "Y", "signal_value": "valueY1"},
{"signal_name": "Z", "signal_value": "valueZ1"},
{"signal_name": "X", "signal_value": "valueX2"}]
res = {}
for i in data:
temp = None
for k, v in i.items():
if k == "signal_name":
try:
res[v]
except:
res[v]=[] #<-- As everytime you were initializing it, this was the error
temp = v
if k == "signal_value":
print(temp)
print(res[temp])
res[temp].append(v)
Ouput will be :
{'X': ['valueX1', 'valueX2'], 'Y': ['valueY1'], 'Z': ['valueZ1']}
I am trying to calculate items within list, adding together string and integer.
Function does not work:
x_list = ['string', '100', 'string']
def calculate_str_and_int():
str_x = x_list[1]
sum_x = int(str_x) + 200
print(sum_x)
Expected output:
['string', 300, 'string']
Thank you in advance!
You should re-assign the value in position 1 to the newly calculated value, like e.g:
x_list = ['string', '100', 'string']
def calculate_str_and_int():
str_x = x_list[1]
sum_x = int(str_x) + 200
x_list[1] = sum_x
print(x_list)
calculate_str_and_int()
This will print:
['string', 300, 'string']
I have a map where a key holds multiple values
datamap = [ 'Antenna Software':[ 'Salarpuria', 'Cessna', 'Vrindavan Tech', 'Alpha Center' ],
'Ellucian':[ 'Malvern', 'Ellucian House', 'Residency Road'] ]
here i need to alphabetically sort the values
datamap = [ 'Antenna Software':[ 'Alpha Center', 'Cessna', 'Salarpuria', 'Vrindavan Tech' ],
'Ellucian':[ 'Ellucian House', 'Malvern', 'Residency Road' ] ]
how to do it in groovy way?
You should be able to do:
def sortedMap = datamap.sort().collectEntries { k, v ->
[ k, v.sort( false ) ]
}
If you're not bothered about sorting the keys of the map, you can get rid of the initial sort():
def sortedMap = datamap.collectEntries { k, v ->
[ k, v.sort( false ) ]
}
Explanation of sort( false ):
By default, the sort method in Groovy changes the original list, so:
// Given a List
def a = [ 3, 1, 2 ]
// We can sort it
def b = a.sort()
// And the result is sorted
assert b == [ 1, 2, 3 ]
// BUT the original list has changed too!
assert a != [ 3, 1, 2 ] && a == [ 1, 2, 3 ]
So if you pass false to sort, it leaves the original list alone, and just returns the sorted list:
// Given a List
def a = [ 3, 1, 2 ]
// We can sort it (passing false)
def b = a.sort( false )
// And the result is sorted
assert b == [ 1, 2, 3 ]
// AND the original list has remained the same
assert a == [ 3, 1, 2 ]