Sum values in a list in python [duplicate] - python-3.x

This question already has answers here:
Python - sum values in dictionary
(5 answers)
Closed 22 days ago.
I'm having a problem in processing lists that I have 2 lists:
shop1= [{'status':'1','price':;1200'},{'status':'1','price':'13000'}] shop2= [{'status':'2','price':3000'},{'status':'2','price':'4000'}]
How can I return the sum of all the prices of shop1, shop2, so?

For calculating dynamic number of shops input you can use itertools with zip_longest method. Hence you don't need to worry if the items length are not same.
import itertools
shop_items = itertools.zip_longest(shop1, shop2)
total = sum([sum(item["price"] for item in items if item) for items in shop_items])

Related

python: remove comboBox items in a loop manner [duplicate]

This question already has answers here:
Python: Removing list element while iterating over list [duplicate]
(9 answers)
List "quirk" in python
(4 answers)
Closed 12 months ago.
I tried to delete all comboBox items using a loop as follow:
for idx in range(comboBox.count()):
print(idx, comboBox.count()) # show the loop number and check how many items left each loop
comboBox.removeItem(idx)
However, still 2 items left after the looping through all items instead of clearing them all out.
The prints are as following.
0 5
1 4
2 3
3 2
4 2
5 2
So it went haywire after 3. Why is this happening?

How to select first row after each 3 rows pandas [duplicate]

This question already has answers here:
Pandas every nth row
(7 answers)
Closed 1 year ago.
I have one dataframe, i want to get first row of each 3 rows in dataframe and save new dataframe
here is input data
df=pd.DataFrame({'x':[1,2,5,6,7,8,9,9,6]})
output:
df_out=pd.DataFrame({'x':[1,6,9]})
Use DataFrame.iloc with slicing:
print (df.iloc[::3])
x
0 1
3 6
6 9

pandas time series offset in months using frequencies notation [duplicate]

This question already has an answer here:
How can I get pandas Timestamp offset by certain amount of months?
(1 answer)
Closed 2 years ago.
I'd like to offset a date in a DatetimeIndex by a certain number of months using a string but can only find reference to the MonthEnd version.
Here is my (incorrect) attempt
rng = pd.date_range('2020-01-01','2020-07-03')
rng[-1] - pd.tseries.frequencies.to_offset('2m')
This will return Timestamp('2020-05-31 00:00:00') when instead I'm looking for Timestamp('2020-05-03 00:00:00')
Is there an alternative to '2m' here which will give me that result?
The DateOffset method in pandas works well:
rng[-1] - pd.DateOffset(months=2)
Output:
Timestamp('2020-05-03 00:00:00')

python dataframe - grouping rows [duplicate]

This question already has answers here:
Pandas: groupby column A and make lists of tuples from other columns?
(4 answers)
Closed 3 years ago.
I'm new to python so the question might not be so clear.
I have this dataset with pandas dataframe that goes something like this.
Id item
0 A0029V93 B0239WN
1 A0029V93 B0302SS
2 A02948s8 B0029ST
...
and the result I want is
Id item
0 A0029V93 (B0239WN,B0302SS)
1 A02948s8 (B0029ST, ...)
2 ... ...
...
No duplicate Id and all the items in the data paired with the ID
It doesn't necessarily have to look like this
as long as I can get the Id,[item] data.
df.groupby('Id')['item'].apply(list)

NA values on Dataframe [duplicate]

This question already has answers here:
How to drop rows of Pandas DataFrame whose value in a certain column is NaN
(15 answers)
How to replace NaN values by Zeroes in a column of a Pandas Dataframe?
(17 answers)
Closed 4 years ago.
so I'm working with python and data frames.
I have a list of movies from which I create a subset and I'm trying to plot or just figure out the mean for that matter but there is a lot of missing information and the frame just has an "NA" instead of data.
I use np library to ignore does values but I still get an error saying the type of input is not supported by isfinite
data = pd.read_csv("C:\\Users\Bubbles\Documents\CS241\week 13\movies.csv")
Action = data[(data.Action == 1)]
Action = Action[np.isfinite(Action.budget)]
print(Action.budget.mean())
the budget list would just contain "NA" and integers as possible values

Resources