I working with a forex dataset, trying to fill in my dataframe with open, high, low, close updated every tick. Here is my code: So as you can see, with for loop I’m getting groups. Now I want to fill the following columns in my dataframe: idx be my df[‘candle_number’] df[‘1h_open’…
Tag: dataframe
Remove default formatting in header when converting pandas DataFrame to excel sheet
This is something that has been answered and re-answered time and time again because the answer keeps changing with updates to pandas. I tried some of the solutions I found here and elsewhere online and none of them have worked for me on the current version of pandas. Does anyone know the current, March 2019,…
Rename dataframe columns with a mapper function that takes parameters
How can I pass along parameters to a mapper function in pandas.DataFrame.rename? The example below uses a mapper function test. I want to change it’s behavior based on an additional parameter that I pass along. In this example, the mapper function appends an “A” to each column name. I want t…
Python DataFrame: How to connect different columns with the same name and merge them into one column
Problem I have a df that has many columns with the same column name. I wish to use the same column name as a key to do like UNION in SQL. Example see example data: df: df.T: I need to combine the two y columns since I want to calculate how many times the words in y leads to the
pandas df – sort on index but exclude first column from sort
I want to sort this df on rows (‘bad job’) but I want to exclude the first column from the sort so it remains where it is: expected output: I don’t know to edit my code below to exclude the 1st column from the sort: Answer Use argsort with add 1 for possible add first value 0 by reindex for
Check if all values in dataframe column are the same
I want to do a quick and easy check if all column values for counts are the same in a dataframe: In: Out: I want just a simple condition that if all counts = same value then print(‘True’). Is there a fast way to do this? Answer An efficient way to do this is by comparing the first value with
difference between “&” and “and” in pandas
I have some code that runs on a cron (via kubernetes) for several months now. Yesterday, part of my code didn’t work that normally does: This statement, all of a sudden, wasnt ‘True’ (both df_temp and df_temp4 have data in them: however, this worked: Was there some sort of code push that wou…
GroupBy columns on column header prefix
I have a dataframe with column names that start with a set list of prefixes. I want to get the sum of the values in the dataframe grouped by columns that start with the same prefix. The only way I could figure out how to do it was to loop through the prefix list, get the columns from the dataframe
Search for a value anywhere in a pandas DataFrame
This seems like a simple question, but I couldn’t find it asked before (this and this are close but the answers aren’t great). The question is: if I want to search for a value somewhere in my df (I don’t know which column it’s in) and return all rows with a match. What’s the most…
Using result_type with pandas apply function
I want to use apply on a pandas.DataFrame that I created, and return for each row a list of values, where each value is a column in itself. I wrote the following code: When I add result_type=’expand’ in order to change the returned array into separate columns I get the following error: However if …