get new list from pivot_table? - python-3.x

I'm using python 3 and pandas. I have this pivot table below.
>>> print(p.head())
question_id 1 2 3 4 ... 26 27 28 29
assessment_attempt_id
...
243908 21-24 Female 4th year undergraduate White ... Disagree Disagree Agree Agree
290934 25-29 Male Prefer Not to Answer Black or African American ... Neutral Neutral Neutral Neutral
312457 18-20 Female 1st year undergraduate White ... Strongly Agree Strongly Agree Strongly Agree Strongly Agree
312766 18-20 Female 2nd year undergraduate Hispanic or Latina/o ... Agree Agree Agree Agree
312786 21-24 Female 4th year undergraduate Black or African American ... Strongly Disagree Agree Agree Agree
It is produced from this command:
p= pandas.pivot_table(df, index=["assessment_attempt_id"], columns=["question_id"], values="text", aggfunc='first')
The table is basically exactly what I want. Now I just want columns 1,2,3,4 and the assessment_attempt_id column in a Datatable, so I can join that data by assessment_attempt_id with another existing data table.
Normally I would subset the data by doing something like this:
df1 = df[['a','b']]
but that produces and error:
KeyError: "['1' '2'] not in index"
It seems like this should be a simple and solved problem but I can not find the answer. I also tried a groupby variation which produced the same output, and also I could not extract the columns I wanted. I assume I can't at the multi-index, but I don't know how. Thank you.

Related

How to sum through two tables in Excel

I am looking for help to sum values in a table using a mapping table..
Tried Xlookup and sumif. Not sure where I am going wrong.
Simplifying it in an example
Basically, I have two tables.
Food type
Food
Fruits
Apple
Fruits
Orange
Vegetable
Spinach
Vegetable
Fenugreek
Food
Cost
Apple
$10
Orange
$15
Spinach
$20
Fenugreek
$10
So, if my input is Foodtype = Fruits, then I should get an answer of $25 (as it will sum up Apple and Orange)..
The combination of sumifs and xlookup is only giving one answer ($10) whereas I need $25.
please let me know.
I cannot combine the tables, as I have to use the first mapping table in multiple places.
Thankyou!
You can try:
=SUM(SUMIF(E2:E5,FILTER(B2:B5,A2:A5=I2),F2:F5))
This uses XLOOKUP and SUM.
=SUM(XLOOKUP(FILTER($B$2:$B$11,$A$2:$A$11=G2),$D$2:$D$5,$E$2:$E$5))

find specific cells according to an order with constraints

i need help on excel
i have table looking like this(it's an example):
cheese tomato chocolate cream
Pizza 2 3
cake 1 1
And i want to have names of ingredients for each plates from the highest one to the lowest if we consider that the numbers are the lead time for each ingredient and if the lead time is the same i don't want to have the same name in both cells.
like this:
Ingredient 1 Ingredient 2 ingredient3
pizza tomato cheese nothing
cake chocolate cream
thank you, please help :)
Try the formula as shown in the image,
=IFERROR(INDEX($B$1:$E$1,MATCH(LARGE($B2:$E2,COLUMN(A1)),$B2:$E2,0)),"nothing")
Am not sure how you want to represent data if the lead time is same. The formula currently shows chocolate twice. You may have to alter it according to your needs. Drag the formula to right and then down for the complete range.

Equal distribution formula based on preset values. Excel

e.g. There are 300 apples. There are 100 people. Each person has a preset value (represented as a number 1 to 5). 1=1 apple, if their value is 5 they get 5 apples etc. But there are 300 apples available so each person is going to get more then the value says they "deserve". Or one day there are only 200 apples and every one gets less then what the value states says they "deserve". Is this possible in excel?
NAME VALUE
john 5
james 5
sam 4
matt 5
mike 3
steve 2
etc...
This absolutely sounds like a perfect problem for Solver to handle. As you know, this in included within Excel's addins. It can deal with all the variables you mentioned.

Lookup with multiple criteria, one a MAX value

I am trying to lookup the LOCATION of an employee (NAME) and their MANAGER from the most recent month (largest month number) in a particular QUARTER in data like this:
NAME LOCATION MANAGER QUARTER MONTH
Ryan Smith Sioux Falls Rick James 3 7
Jane Doe Tampa Bobby Brown 3 7
John Rogers Tampa Tracy Lane 3 7
Ryan Smith Sioux Falls Rick James 3 8
Jane Doe Denver Thomas Craig 3 8
John Rogers Tampa Cody Davis 3 8
So if I know the name of the employee and the quarter I'm looking up, the results should display who their last manager was and the location they were in, as these may change month to month.
I have used an INDEX and MATCH array formula:
{=INDEX($B$2:$B$7,MATCH(A12,IF($D$2:$D$7=D12,$A$2:$A$7),0))}
but this just provides the first match and not necessarily the most recent month in that quarter. I attempted to include a MAX function which looked something like this:
{=INDEX($B$2:$B$7,MAX($E2:$E7,MATCH(A12,IF($D$2:$D$7=D12,$A$2:$A$7),0)))}
but that didn't quite get me there either.
What formula do I need to get this to work?
I think I'd choose a PivotTable for its versatility and speed:
I think a pivot table is probably the best option and can easily be modified with the filters when new entries are added to the underlying data. I was working on a solution with a formula, but it requires you to add a lookup column.
The formula for the lookup column is: =E6&" "&H6&" "&I6
I wasn't clear on how the OP was going to be "entering" his employee name and quarter, so I had to make an assumption that it would be in a separate column:
And the formula in column B (which is cumbersome) is:
=VLOOKUP(A6&" "&MAX(IF(H1:H100=NUMBERVALUE(RIGHT(A6,1)),I1:I100)),$D$6:$G$11,3,FALSE)&", managed by "&VLOOKUP(A6&" "&MAX(IF(H1:H100=NUMBERVALUE(RIGHT(A6,1)),I1:I100)),$D$6:$G$11,4,FALSE)
But it works, and as long as the lookup range is adjusted, is scaleable.

Extracting data from a non-formatted string

I want to extract certain parts and be able to put it into a nice spreadsheet format. The important parts are the address, ward number, square feet, and price. I was going to try something really complicated in PHP(novice), but thought there might be an easier way.
The data looks like this:
243-467
1402 E. Mt. Pleasant Ave. 50th Ward approximately 1,416 sq. ft. more or less BRT# 502440300 Improvements: Residential Dwelling
JANET DENNIS C.P. October Term, 2007 No. 01082 $105,641.01 Morton R. Branzburg, Esq.
244-712A
5407 Chestnut St. - Premise A 60th Ward Apt 2-4 Unts 2 sty Masonry; Improvement Area 4,610 sq. ft. BRT# 603011200 Improvements: Residential Dwelling
ALEXANDER TALMADGE, JR. (WHO HAS 1/3 INTEREST), BERNADINE ABAD AND BERNARD BLAIR TALMADGE $32,153.00 Drew Salaman, Esq.
Where does the data come from? Can you modify its output? If so, try outputting CSV text (http://en.wikipedia.org/wiki/Comma-separated_values). Excel will import CSV files.

Resources