name_list=['William','Laura','Robert','Alicia','Sharon','Jack','Mary','Edward','Jessie','Debra']
day_list=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
hormones_list=[name_list,day_list]
print(hormones_list[0][0])
I wrote the one-dimensional ones. But how can I write the 2d? When I write hormone_list[0][0] it has to show william's monday data. But I don't know how. Also how can I temporarily store seperate lists? Should I use if structures without writing them one by one? It says shortest so I'm a bit cautious.
You can nest lists, so instead of [value, value, value] you'd use [list, list, list].
So the values from monday-sunday would be a list and then you'd add all these list in a row.
Like:
hormone_list = [[1,2,3,4,5,6,7],[2,3,4,5,6,7,8], ...]
This is likely not how I would solve the problem if presented as a business case but the answer seems to ask for lists and comprehensions so I might do:
## ----------------------------
## Our data
## ----------------------------
name_list=['William', 'Laura', 'Robert', 'Alicia', 'Sharon', 'Jack', 'Mary', 'Edward', 'Jessie', 'Debra']
day_list=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
hormones_list=[
[1.0, 1.3, 1.7, 1.1, 1.8, 1.3, 1.2], # William's daily readings
[1.9, 1.9, 1.5, 1.8, 1.9, 1.7, 1.5], # Laura's daily readings
[3.0, 3.3, 3.1, 2.7, 3.5, 4.5, 4.7], # Robert's daily readings
# ....
]
## ----------------------------
print(f"Williams reading on Monday was: {hormones_list[0][0]}")
## ----------------------------
## build the list of high days using a list of lists like hormones_list.
## Note: I would probably use a days_high dictionary with something like
## days_high.setdefault(patient_index, []).append(day_index)
## ----------------------------
days_high = [
[
day_index
for day_index, day_reading
in enumerate(patient_readings)
if day_reading > 4.0
]
for patient_readings
in hormones_list
]
## ----------------------------
## ----------------------------
## Print out the warnings for a given patient (if there are any)
## ----------------------------
for patient_index, patient_readings in enumerate(days_high):
if not patient_readings:
continue
patient_name = name_list[patient_index]
days = " ".join([day_list[i] for i in patient_readings])
print(f"WARNING: High values for {patient_name}: {days}")
## ----------------------------
giving the following output:
Williams reading on Monday was: 1.0
WARNING: High values for Robert: Saturday Sunday
Related
I have a data frame as shown below.
df:
id fcb psg rma
1 4.0 2.9 4.1
2 3.5 4.2 3.5
3 2.5 4.5 4.0
4 4.1 4.6 4.2
I desired to see all of the ratings in a single column. So I ran the code below.
df.set_index(['id']).apply(dict, axis=1).reset_index(name='ratings')
However, I obtained the following results. But I wanted to remove the letter 'u" from all of the keys in each dictionary.
id ratings
1 {u'fcb': 4.0, u'psg': 2.9, u'rma': 4.1}
2 {u'fcb': 3.5, u'psg': 4.2, u'rma': 3.5}
3 {u'fcb': 2.5, u'psg': 4.5, u'rma': 4.0}
4 {u'fcb': 4.1, u'psg': 4.6, u'rma': 4.2}
Expected output:
id ratings
1 {'fcb': 4.0, 'psg': 2.9, 'rma': 4.1}
2 {'fcb': 3.5, 'psg': 4.2, 'rma': 3.5}
3 {'fcb': 2.5, 'psg': 4.5, 'rma': 4.0}
4 {'fcb': 4.1, 'psg': 4.6, 'rma': 4.2}
I tried below code to eliminate leading unicode in each keys.
df['rec_dict'] = df['rec_dict'].apply(lambda x: {str(k[1:]): v for k, v in x.items()})
Try this
df = df.set_index(['id']).apply(dict, axis=1).reset_index(name='ratings')
df['ratings'] = df['ratings'].apply(lambda x: {float(k[1:]): v for k, v in x.items()})
The first line is the same as your original code, but the second line uses the apply() function along with a lambda function to iterate over each dictionary in the 'ratings' column, and for each key-value pair, it converts the key from a string to a float and removes the 'u' from the beginning of the key. The resulting dictionary is then assigned back to the corresponding row in the 'ratings' column.
u'string' denotes a unicode string in Python. Since Python 3 all strings have unicode encoding by default. So you can safely ignore the notation.
Iv'e got the following hardcoded list of tuples:
dates = [
('2020-01', '2020-02'),
('2020-02', '2020-03'),
('2020-03', '2020-04'),
('2020-04', '2020-05'),
('2020-05', '2020-06'),
('2020-06', '2020-07'),
('2020-07', '2020-08'),
('2020-08', '2020-09'),
('2020-09', '2020-10'),
('2020-11', '2020-12'),
('2020-12', '2021-01'),
('2021-01', '2021-02'),
('2021-02', '2021-03'),
('2021-03', '2021-04'),
('2021-04', '2021-05'),
('2021-05', '2021-06'),
('2021-06', '2021-07'),
('2021-07', '2021-08'),
('2021-08', '2021-09'),
('2021-09', '2021-10'),
('2021-11', '2021-12'),
('2021-12', '2022-01'),
('2022-01', '2022-02'),
]
Assuming we were in January 2022, this list packs a tuples of ("from_date", "to_date") of 2 consecutive years (going back).
I was wondering if there's a way of getting this dates list in a generic and elegant manner from todays month which means going backwards two years from today.
Thanks!
I have a column, 'EDU', in my dataframe, df. where I tried to create a dictionary with value_counts(), poe_dict. It looks like this.
edu_m=df['EDU'].sort_values()
poe_dict = edu_m.value_counts(normalize=True).to_dict()
poe_dict
{4: 0.47974705779026877,
3: 0.24588090637625154,
2: 0.172352011241876,
1: 0.10202002459160373}
Now, I'm trying to replace the keys '4,3,2,1' with these strings which I put in a list.
n_keys=["college","more than high school but not college","high school","less than high school"]
If I do each of them individually, this runs ok, giving me the expected result.
In:
poe_dict['college'] = poe_dict.pop(4)
poe_dict['more than high school but not college'] = poe_dict.pop(3)
poe_dict['high school'] = poe_dict.pop(2)
poe_dict['less than high school'] = poe_dict.pop(1)
Out:
{'college': 0.47974705779026877,
'more than high school but not college': 0.24588090637625154,
'high school': 0.172352011241876,
'less than high school': 0.10202002459160373}
however, if I try to do it as a loop, it produces this.
In:
for key, n_key in zip(poe_dict.keys(), n_keys):
poe_dict[n_key] = poe_dict.pop(key)
poe_dict
Out:
{2: 0.172352011241876,
1: 0.10202002459160373,
'high school': 0.47974705779026877,
'less than high school': 0.24588090637625154}
So I dont understand why the loop does not work for keys 2 and 1?
I have tried to debug it as well to see what happens in the loop like this.
In:
for key, n_key in zip(poe_dict.keys(), n_keys):
print (key,n_key)
poe_dict[n_key] = poe_dict.pop(key)
Out:
4 college
3 more than high school but not college
college high school
more than high school but not college less than high school
You loop over the keys of poe_dict in the for loop. However the keys of poe_dict is modified when the statement is poe_dict[n_key] = poe_dict.pop(key) has been run. Therefore, the keys information gets wrong. The correct way is to store the keys of peo_dict into a list list(poe_dict.keys()) and loop over this new list of keys.
poe_dict = {4: 0.47, 3:0.25, 2:0.17, 1:0.10}
n_keys = ['college', 'more than high school but not college','high school', 'less than high school' ]
keylist = list(poe_dict.keys())
for key, n_key in zip(keylist, n_keys):
print (key,n_key)
poe_dict[n_key] = poe_dict.pop(key)
print (poe_dict)
The results will be
{'college': 0.47, 'more than high school but not college': 0.25, 'high school': 0.17, 'less than high school': 0.1}
I am trying to create a funnel chart using plotly and am not having any luck. Even the canned examples from plotly don't work for me, can someone please help?
from plotly import graph_objects as go
fig = go.Figure(go.Funnel(
y = ["Website visit", "Downloads", "Potential customers", "Requested price", "invoice sent"],
x = [39, 27.4, 20.6, 11, 2]))
fig.show()
I get this huge traceback error:
lueError:
Invalid value of type 'plotly.graph_objs.Funnel' received for the 'data' property of
Received value: Funnel({
'x': [39, 27.4, 20.6, 11, 2],
'y': [Website visit, Downloads, Potential customers, Requested price, invoice
sent]
})
The 'data' property is a tuple of trace instances
that may be specified as:
- A list or tuple of trace instances
(e.g. [Scatter(...), Bar(...)])
- A list or tuple of dicts of string/value properties where:
- The 'type' property specifies the trace type
One of: ['area', 'bar', 'barpolar', 'box',
'candlestick', 'carpet', 'choropleth', 'cone',
'contour', 'contourcarpet', 'funnel',
'funnelarea', 'heatmap', 'heatmapgl',
'histogram', 'histogram2d',
'histogram2dcontour', 'isosurface', 'mesh3d',
'ohlc', 'parcats', 'parcoords', 'pie',
'pointcloud', 'sankey', 'scatter',
'scatter3d', 'scattercarpet', 'scattergeo',
'scattergl', 'scattermapbox', 'scatterpolar',
'scatterpolargl', 'scatterternary', 'splom',
'streamtube', 'sunburst', 'surface', 'table',
'violin', 'volume', 'waterfall']
- All remaining properties are passed to the constructor of
the specified trace type
(e.g. [{'type': 'scatter', ...}, {'type': 'bar, ...}])
It seems like you’re using version 3, in which case you will need to use go.Figure(data=[go.Funnel(...)]) (basically wrap your funnel trace in an array... only in version 4 is this optional)
I'm trying to feed a record to knn.predict() to make a prediction by using the following code:
person_features = {
'cma': 462, # Metropolitan area
'agegrp': 9, # Age Group
'sex': 1,
'ageimm': 6, # Age group at immigration
'immstat': 1, # Immigrant status
'pob': 21, # Other Eastern Asia
'nol': 4, # First languages
'cip2011': 7, # Major field of study: Mathematics, computer and information sciences
'hdgree': 12, # Hightest Education
}
prediction = knn.predict(person_features)
labels={True: '>50K', False: '<=50K'}
print(labels[prediction])
But it showed
TypeError: float() argument must be a string or a number, not 'dict'
I tried making it into list of tuples like:
person_features= [('cma',462), ('agegrp',9), ('sex',1), ('ageimm',6), ('immstat',1), ('pob',21), ('nol',4), ('cip2011',7), ('hdgree',12)])
But didnt work either.
What should I do to solve this type error? I feel like the solution is easy, but somehow I just could wrap my head around it.
New to programming and just started to learn Python less than three month. So bear with me for my amateur questions and answer!
# I looked up the numbers from the coding book
cma = 462
agegrp = 9
sex = 1
ageimm = 6
immstat = 1
pob = 21
nol = 4
cip2011 =7
hdgree = 12
MoreThan50K = 1 # what I am going to predict, 1 for >50K, 0 for <50K
person_features = [cma, agegrp, sex, ageimm, immstat, pob, nol, cip2011, hdgree, MoreThan50K]
prediction = knn.predict(person_features)
So it was pretty straightforward afterall.