If I have the following dataframe: ID other 219218 34 823#32 47 unknown 42 8#3#32 32 1#3#5# 97 6#3### 27 I want to obtain the following result: ID other 219218 34 823#32 47 unknown 42 8#3#32 32 unknown 97 unknown 27 I am using the following code which works. Is there a way to make it more optimal, bearing in
Tag: dataframe
Update different values in a column based on multiple conditions
I’d like to update values in a column [‘D’] of my data frame based on the substrings contained in column [‘A’] I have dataframe df, where column D is an exact copy of column A. Column A has list of different cereal names. But I want to check if A contains a certain word and if so…
pandas groupby ignoring column
Below is my groupby function and dataset before the operation. However, the statement as written produces no change. I want this to be a single row containing sums for each category. Answer You need to enclose your list by []:
pandas groupby create new columns based on col1 containing value of col2
I have a pandas dataframe that I want to group by and create columns for each value of col1 and they should contain the value of col2. And example dataframe: I want to groupby item_id, create as many columns as feature_category_id and fill them with the feature_value_id. The resultant df for the example would…
Pandas Dataframe into nested Dictionaries conversion in Python
I need a little help. I want to convert from dataframe into nested dictionaries. and i want to convert in this format: Answer We can do groupby with agg dict items
Pandas pivot table count
I would like to ask a question concerning pivot tables in Pandas. I have been trying to make a pivot table for this kind of table: sector score US null US null US 1 EU null EU 2 EU 2 EU 4 UK null UK null UK null UK 4 UK 4 Eventually, I would like this table to be
How to get first and last value of each group in pandas with no group by column?
Hi Folks, I need to take first and last value from each group(where the counter value is 1 consecutively ) My Input :- Desired Output :- Answer You can aggregate by consecutive 1 values with aggregate minimal and maximal TIMESTAMP: EDIT: Test groups:
Remove duplicate substring at the start of the string
I would like to remomve duplicate substrings at the start of a string where a duplicate exists. I sort of have the logic working for the first row (see below) but am quite new to Python so am struggling to produce code which will apply the same logic for a rows in a larger dataset. Below is an example of:
Pandas – stack time columns with time and date
I have date and time data now I want to reduce this dataframe to two columns with Timestamp (date+time) in a column and value in another column current df – desired df – Here is original list from which I’m creating my dataframe – Answer Use melt to flatten your dataframe and set Time …
Pandas: using groupby to calculate a ratio by specific values
Hi I have a dataframe that looks like this: and I want to calculate a ratio in the column ‘count_number’, based on the values in the column ‘tone’ by this formula: [‘blue’+’grey’]/’red’ per each unite combination of ‘participant_id’, R…