Column names are: ID,1,2,3,4,5,6,7,8,9. The col values are either 0 or 1 My dataframe looks like this: I want the column names in front of the ID where the value in a row is 1. The Dataframe i want should look like this: Please help me in this, Thanks in advance Answer set_index + stack, stack will dropna by default
Tag: dataframe
How to plot aggregated by date pandas dataframe
I have this dataframe To aggregate payout_value by date I use: How do I plot (bar chart) dates on x-axis and aggregated payout sum on y axis? I tried using df.plot(x=’date’, y=’payout_value’,kind=”bar”) approach, but there is no ‘date’ column in df_daily dataframe, print(list(df_daily)) gives [(‘payout_value’, ‘sum’)] Answer you are almost there, use reset_index and plot your by df_daily
Save and export dtypes information of a python pandas dataframe
I have a pandas DataFrame named df. With df.dtypes I can print on screen: I want to save this information so that I can compare it with other data, type-cast things elsewhere, etc. I want to save it into to a local file, recover it elsewhere in another program where the data can’t go. But I’m not able to figure
How do I convert a Pandas dataframe to a PyTorch tensor?
How do I train a simple neural network with PyTorch on a pandas dataframe df? The column df[“Target”] is the target (e.g. labels) of the network. This doesn’t work: Answer I’m referring to the question in the title as you haven’t really specified anything else in the text, so just converting the DataFrame into a PyTorch tensor. Without information about
Python Dataframes: Describing a single column
Is there a way I can apply df.describe() to just an isolated column in a DataFrame. For example if I have several columns and I use df.describe() – it returns and describes all the columns. From research, I understand I can add the following: “A list-like of dtypes : Limits the results to the provided data types. To limit the
pandas DataFrame: normalize one JSON column and merge with other columns
I have a pandas DataFrame containing one column with multiple JSON data items as list of dicts. I want to normalize the JSON column and duplicate the non-JSON columns: I want I can normalize JSON data using: but I don’t know how to join that back to the id column of the original DataFrame. Answer You can use concat with
How to remove timezone from a Timestamp column in a pandas dataframe
I read Pandas change timezone for forex DataFrame but I’d like to make the time column of my dataframe timezone naive for interoperability with an sqlite3 database. The data in my pandas dataframe is already converted to UTC data, but I do not want to have to maintain this UTC timezone information in the database. Given a sample of the
How to show all columns’ names on a large pandas dataframe?
I have a dataframe that consist of hundreds of columns, and I need to see all column names. What I did: The output is: How do I show all columns, instead of a truncated list? Answer You can globally set printing options. I think this should work: Method 1: Method 2: This will allow you to see all column names
TypeError: ‘Series’ object is not callable when accessing dtypes of a dataframe
As it is [Duplicate], I have raised this to be removed. Kindly do not rollback. Answer There’s no ambiguity here. file is a dataframe, and dtypes is an attribute. When you access dtypes, a Series is returned: When you call df.dtypes(), you are effectively doing series = df.dtype; series() which is invalid, since series is an object (not a function,
Sort a pandas dataframe series by month name
I have a Series object that has: Problem statement: I want to make it appear by month and compute the mean price for each month and present it with a sorted manner by month. Desired Output: I thought of making a list and passing it in a sort function: but the sort_values doesn’t support that for series. One big problem