How to populate a dataframe from row-by-row calculations?
I am seeking to populate a pandas dataframe row-by-row, whereby each new row is calculated on the basis of the contents of the previous row. I am using this for simple financial projections.
I am seeking to populate a pandas dataframe row-by-row, whereby each new row is calculated on the basis of the contents of the previous row. I am using this for simple financial projections.
Code import pandas as pd import numpy as np df = pd.DataFrame({"K1":[1,2,3],"K2":[4,5,6]}) df["result"] = (df["K1"].shift(1)*df["K2"]).fillna(1)[::-1] #line A print(df["result"]) print((df["K1"].shift(1)*df["K2"]).fillna(1)[::-1]) #line B print((df["K1"].shift(1)*df["K2"]).fillna(1)) # line C Output 0 1.0 1 5.0 2 12.0 Name: result, dtype: float64 2 12.0 1 5.0 0 1.0 dtype: float64 0 1.0 1 5.0 2 12.0 dtype: float64 Why column result … Read more
I have a 20*5 data table and I want to find the mean value of one of the columns which is the price column. I know I have to use this method for finding the mean value
I’m a beginner in Pandas. I have a data file containing 10000 different information of users. This data contain 5 columns and 10000 rows. One of these columns is the district of the users and it divides users according to their living place(It defines just 7 different locations and in each of locations some number of users live). as an example, out of this 10000 users, 300 users live in USA and 250 Live in Canada and..
I want to define a DataFrame which includes five random rows of users with the distinct of: USA,Canada,LA,NY and Japan. Also, the dimensions needs to be 20*5. Can you please help me how to do that?
I know for choosing random I need to use
In the below dataframe the column “CumRetperTrade” is a column which consists of a few vertical vectors (=sequences of numbers) separated by zeros. (= these vectors correspond to non-zero elements of column “Portfolio”). I would like to find the cumulative local maxima of every non-zero vector contained in column “CumRetperTrade”.
To be precise, I would like to transform (using vectorization – or other – methods) column “CumRetperTrade” to the column “PeakCumRet” (desired result) which gives for every vector ( = subset corresponding to ’Portfolio =1 ’) contained in column “CumRetperTrade” the cumulative maximum value of (all its previous) values. The numeric example is below. Thanks in advance!
PS In other words, I guess that we need to use cummax() but to apply it only to the consequent (where ‘Portfolio’ = 1) subsets of ‘CumRetperTrade’
Consider the following query:
Suppose I have the following dataframe where the first column is a facility and the second column is a product produced by the facility.
Below is my DF df = pd.DataFrame({'A': ['a', 'b', 'c', '0'], 'B': ['0xF188-abc-cde', '0xF188-abc-abcde', '0xF188-abc-1234', '0xF188-abc-tu231er']}) Now I want to add NEW column “EXTRACT” which is an extraction of column ‘B’ after second hyphen. Below is the Expected Column. df= pd.DataFrame({'A': ['a', 'b', 'c', '0'], 'B': ["0xF188-abc-cde", '0xF188-abc-abcde', '0xF188-abc-1234', '0xF188-abc-tu231er'], 'Extract':['cde', 'abcde', '1234', 'tu231er']}) Answers: … Read more
Thank you for your time. I am not an advanced programmer. I am taking 1 programming course. I understand the basics and an okay amount of Python. Please don’t destroy the little confidence I have in programming. I realize the answer may exist but I haven’t found it yet with my searching skills.
I am analysing eye-tracking data. I have a df with a column ‘row’ which tells me which image is looked at. I have 9 images which belong to 3 categories.