Grist: lookup value from another table - python

Attempting to learn Grist, and don't know Python...but willing to learn. Just trying to draw the lines between Python and formulas.
I have a table that has "Items": fields named "ProductID" "collection" & "buyer"
There is another table that is named "Sales": fields named "Sku"(same as ProductID) "Qty" "Cost" "Sales" "Date"
I would like to create another table, that consolidates the data into one document (since all of sales may not be in all of items, and sales has a ton of duplicates due to the date the transaction occurred.)
Something like: "Sku" "Buyer" "collection" "Qty" "Cost" "Sales" "margin"(formula to calculate)
"Sales" would need to be the root table, and reference the "items" table for more information.
If my data was smaller, in excel I would:
copy skus, paste in new tab, remove duplicates, and run a sumifs.
ex: if in cell B1 and sku is in a1:
=Sumifs(sales!$Qty, sales!$sku, A1)
Then I would run an index match on items in c1 for example:
=index(items!$Buyer, match(a1, Items!$ProductID, 0), 1)

(Very late, but answering in case it helps others.)
It sounds like the resulting table should have one record per Sku (aka ProductId). You can do it in two ways: as another view of the Items table or as a summary of the Sales table grouped by Sku.
In either case, you can pull in the sum of Qty or Cost as needed.
In case of a summary table, such sums get included automatically (more on that in https://support.getgrist.com/summary-tables/#summary-formulas).
If you base it on the Items table, you can look up relevant Sales records by adding a formula column (e.g. named sales) with formula:
Sales.lookupRecords(Sku=$ProductID)
Then you can easily add sums of Qty and Cost as columns with formulas like SUM($sales.Qty) or SUM($sales.Cost).

Related

how to get the ids of the row after using to_frame()

after using to_frame(), how do I get the id of the row.
For example:
After to_frame()
column_name
1
35
2
34
If this is the table, is there a way to get the id 1 or 2
Edit:
What I am trying to do is I got all the unique product/Item in the data and stored the data into another table named stock.
I then checked how many of the products were sold, so I used value_count.to_frame() to store the value_counts as its own column. However, when I run this code, the stock name becomes the id, which is not what I wanted. I wanted the stock name and the values to count on a separate column in the same table. Is there a way to do this?

How to create a list of every items purchased by client X?

How can I create a list of every item purchased by the client ID that I specify?
This question is part of the recommendation system I am building.
My dataframe has 3 columns ['ClientID'],['Products'],['Ratings']
(A 'Rating' different than NaN represent an product purchased)
So far I have wrote this code:
First, I created a pivot table with ['ClientID'] on the vertical index and ['Products'] on the horizontal index and ['Ratings'] as the values.
piv = datacf1.pivot_table(index=['ClientID'], columns=['Products'], values=['Ratings'])
#Drop all columns containing only zeros, representing users who did not rate/purchased
piv.fillna(0, inplace=True)
piv = piv.T
piv = piv.loc[:, (piv != 0).any(axis=0)]
The following code uses the pivot table to get the list of purchased items by client X
#Create a list of every products traded by user X
purchased = piv.T[piv.loc[5039595.0,:]>0].index.tolist()
purchased
5039595 is the ['ClientID'] for which I want to create my list of items purchased and I would like to apply a different ['ClientID'] on demand.
I get an error when running the code to create the list.
Why I believe the 'create a list' code gives me an error:
I believe this code reads the vertical index as ['0','1','2','3',...] so it expects to find the column '5039595', however as said previously the vertical index represents the ['ClientID'] which are random.
Here is a snapshot of the vertical index ['ClientID'] of the pivot table:
How can I fix my code to look for the Client X that I want to create the list for?
Or is there another way to do it? Perhaps with my original dataset with 3 columns ['ClientID'],['Products'],['Ratings']
I think the best way to do this is to select all the rows containing purchases for a given customer then take all the unique product values from those rows. It would look something like this:
desired_rows = (data["ClientID"].isin([client_id]) & data[Ratings].notnull())
product_list = data.loc[desired_rows, "Products"].unique().tolist()
print(product_list)

Copying columns data to a new sheet using a reference column

Here is a snip of my data for reference
Column A contains 9134 study IDs and column B contains 9467 study IDs. I have previously applied exclusions to column A (it was once column B before I deleted certain people due t exclusion) Columns C-G is the new data which corresponds to column B (there are 9467 rows of the data)
What I am looking to do is match the data to column A..for example "Study ID 2 #1195" I would like that data to line up with that Study ID 1 #1195 in column A
ALSO, because I intentionally deleted some people from column A, there are Study ID 2 #'s that have been deleted from Study ID 1..so I am not interested in those people
The ultimate goal is to line up the data with column A in order to copy and paste seamlessly into an existing SPSS database
I am not sure how to go about this. Any help would be greatly appreciated!!
HERE IS A LINK TO MY DATASET
https://drive.google.com/open?id=1lhbuthqNPLLi8KRVmCOEqmBkh5dy2C41
This seems like more of an excel question than any kind of coding question. I think all you want to do is keep records where the study id exists in column A.
This can be achieved in Excel y doing a vlookup on column b where you check if a record exists in column A.
You can then filter out any records that don't exist and copy that information to a new sheet.
Example:
Add new column
Apply filter:

Take values from one column and create new column from it

I have big database that has one column called "Measurments" and one column with called "data" which contains data about those different measurments, for example, i measurments you can find height, weight and different indices values and in data you will find the value for this "measurment".
I would like to organize this database in a way that each unique measurment type, will have its' own column, so for example i'll have column name weight, height ect. and the vvalue they got from the column "data".
Until nowI have used this way in order to create many little databases with my relevant data:
df_NDVI=df[(df['Measurement'] == 'NDVI') & (df['Data']!='Corrupt')]
df_VPP_kg=df[(df['Measurement'] == 'WEIGHT')]
But as youcan see, it is not efficient and it creates many databases instead of one with those columns.
My end goal: to take each unique field from "measurments" column and create new column for it with the correct data from column "data".
Try this:
df["obs"]=df.groupby("Measurements")["Measurements"].cumcount()
df.pivot(index="obs", columns="Measurements", values="Data")
So you will get 1 column for each unique value from Measurements, and Data will be order below by order of observation.

Compare two rows in a one2many table in OpenERP6.1

How can we compare two rows in a oneTomany table in OpenERP6.1?
I have a main table, say 'XX' and i have a oneTomany table, say 'YY' corresponding to
that table.
Now, i have three columns in the 'YY' table.Every time i create records into
this table, i want to check if the values in the three columns are identical.
i.e if i click the create button and entered the first row with values
'happy','new','year',
the next time when you enter the same values, you should be prompted with
a message that these values should not be repeated.
you can use at least one of two methods:
1. use _sql_constraint with unique on your columns, like in
_sql_constraints = [
('number_uniq', 'unique(number, company_id, journal_id, type)', 'Invoice Number must be unique per Company!'),
]
from account.invoice object, or
override create/write methods for yy object and write a onchange function for your fields.
Why cant you use _constraint? You will only get warning when you are saving the record.

Categories

Resources