I have a dataframe as follows: And I want to convert this dataframe as follows: I tried a few things but nothing worked. Any ideas? Answer Use Series.str.get_dummies with DataFrame.stack: If order is important:
Tag: dataframe
How drop duplicate rows based on a time delta whilst keep the latest occurrence of that record?
I have a table in the form: ID DATE_ENCOUNTER LOAD 151336 2017-08-22 40 151336 2017-08-23 40 151336 2017-08-24 40 151336 2017-08-25 40 151336 2017-09-05 50 151336 2017-09-06 50 151336 2017-10-16 51 151336 2017-10-17 51 151336 2017-10-18 51 151336 2017-10-30 50 151336 2017-10-31 50 151336 2017-11-01 50 151336 …
Reference DataFrame value corresponding to column header
I am trying to append a column to my DataFrame that is based on the values that are referenced by indicated column names. I have the following DataFrame: The values under “Select” are referencing the values that are under the column number that “Select” shows. For example, for row 0, &…
How do I extract the last trading day from a csv file?
I have a csv file which contains 30 years of stocks data. A portion of it looks like this: How can I extract the last trading date of a specific month of a specific stock, say the last trading month of June 2018 of stock s_0003? This is my code, but it doesn’t seem to be working for me because
Filter and calculate % of a dataframe column
I am trying to calculate the % of men and women in a dataframe column named “gender”. “gender” is defined as an object taking 3 values : “Man” “Woman” “nan” (NA). I did this : But it returns 0.0% for both. When i check ‘total’ value it re…
How do I melt a pandas dataframe?
On the pandas tag, I often see users asking questions about melting dataframes in pandas. I am gonna attempt a cannonical Q&A (self-answer) with this topic. I am gonna clarify: What is melt? How do I use melt? When do I use melt? I see some hotter questions about melt, like: Convert columns into rows with…
inserting missing categories and dates in pandas dataframe
I have the following data frame. I want to add in all score levels (high, mid, low), for each group (a, b, c, d), for all dates (there are two dates – 2020-06-01 and 2020-06-02) I can add in the score categories for all subjects with the following, but i am having trouble adding date in as well the expe…
how to apply Pandas.set_option (Python) to pandas.style objects
I have noticed that when we set some options for pandas DataFrames such as pandas.DataFrame(‘max_rows’,10) it works perfectly for DataFrame objects. However, it has no effect on Style objects. Check the following code : Which will result in : I do not know how to set the properties for Style objec…
Is there anyway to convert specific text data to csv format and give Header names in python?
I have this format of the dataset in a text file. Here the dataset link is https://drive.google.com/file/d/1RqU2s0dqjd60dcYlxEJ8vnw9_z2fWixd/view?usp=sharing I want to give the left side value as column name and right side values will be a row format. Output should be I have tried with text to CSV but not wor…
pandas.Dataframe equivalent for Pandas.read_csv converters?
This discussion covers the differences between dtypesand converters in pandas.read_csv function. I could not find an equivalent to converters for the pandas.DataFrame constructor in the documentation. If I build a dataframe directly from a list of lists, what would be the best way to mimic the same behavior? …