I am trying to read B.txt using pandas. It prints the value of B but not as a list. I present the current and expected outputs. The current output is The expected output is Answer Add squeeze = True for Series, so ouput is B = [‘B=3’], select first value and split, select second value and convert …
Tag: pandas
Iterating row-wise over 2 pandas dataframes and passing these vectors as args to function
I’d like to iterate row-wise over 2 identically-shaped dataframes, passing the rows from each as vectors to a function without using loops. Essentially something similar to R’s mapply. I’ve investigated a little and the best that I’ve seen uses map in a list comprehension, but I’…
Concat string in column values where it is missing in Python
I have a dataframe I want to append string chr in column CHROM where it’s missing. I can do it in R with grepl and paste, but wanted to try in Python. I came up with these two commands, but not sure how to index the column because pd.Series is generating NaNs. Answer String operations in pandas are not …
Pandas Join Two Dataframes According to Range and Date
I have two dataframes like this: I want to bring the RATE values to the second df in accordance with the DATE. Also, the AMOUNT and DAY values in the relevant DATE must be within the appropriate range (MAX_AMOUNT & MIN_AMOUNT, MAX_DAY & MIN_DAY). Desired output like this: Could you please help me abou…
replace whitespace with comma in multiline string (doc string), but keeping end-of-line
I have a multiline string (and not a text file) like this: The column white spaces are unequal. I want to replace the whitespace with a comma, but keep the end-of-line. So the result would look like this: …or alternatively as a pandas dataframe. what i have tried I can use replace(”) with differen…
How to check if a row of a Pandas dataframe has a cell with a specific value and if it does modify the last cell?
I have a dataframe df: name age_5_9 age_10_14 age_15_19 Alice no bones broken no bones broken broke 1 bone Bob no bones broken broke 2 bones no bones broken Charles no bones broken no bones broken no bones broken I would like to create a column broke_a_bone that is 1 when any of the rows has a value ‘br…
How to improve performance of dataframe slices matching?
I need to improve the performance of the following dataframe slices matching. What I need to do is find the matching trips between 2 dataframes, according to the sequence column values with order conserved. My 2 dataframes: Expected output: This is the following code I’ m using: Despite working, this is…
Panel data: take first observation of each group, repeat row and adjust certain values
I have a large Pandas dataframe that looks as follows (85k rows): My goal is the following: For the first observation of each ID for which the BEGDT > Inception, copy the row and change the BEGDT to Inception and the ENDDT to BEGDT – 1 day of the initially copied row. Accordingly, the final output sh…
Vectorization assign the newest value based on datetime
I have two dataframe. The first dataframe have only one column: email, the first dataframe is a complete list of email. The second dataframe is a dataframe with three column: email, subscribe_or_unsubscribe, date. The second dataframe is a history of user subcribing or unsubscribing from the email system. The…
efficient way to find the most recent entry in another dataframe for each entry of a dataframe indexed by datetime in pandas
I have two dataframes, and both of them are indexed by datetime. for example, the dataframe 1 is something below: and the dataframe 2 looks like: For each entry in dataframe 1, I want to find the most recent one entry in dataframe 2, and create a new column in dataframe 1 to setup the relationship between the…