How to group by and dummies in pandas

2024/10/10 4:22:53

I have a pandas dataframe: key val

A    1A    2B    1B    3C    1C    4

I want to get do some dummies like this:

A  1100b  1010c  1001
Answer

Here is something that I used for my case and tried to apply on yours as far as I could understand the problem

dfkey1  key2
0    A     1
1    A     2
2    B     1
3    B     3
4    C     1
5    C     4pd.get_dummies(df, columns=['key2']).groupby(['key1'], as_index=False).sum()

Output:

  key1  key2_1  key2_2  key2_3  key2_4
0    A     1.0     1.0     0.0     0.0
1    B     1.0     0.0     1.0     0.0
2    C     1.0     0.0     0.0     1.0
https://en.xdnf.cn/q/69940.html

Related Q&A

Iterate over a dict except for x item items

I have a dict in this format:d_data = {key_1:value_1,key_2:value_2,key_3:value_3,key_x:value_x,key_n:value_n}and I have to iterate over its items:for key,value in columns.items():do somethingexcept for…

Best way to do a case insensitive replace but match the case of the word to be replaced?

So far Ive come up with the method below but my question is is there a shorter method out there that has the same result?My Code :input_str = "myStrIngFullOfStUfFiWannAReplaCE_StUfFs" …

Given a list of numbers, find all matrices such that each column and row sum up to 264

Lets say I have a list of 16 numbers. With these 16 numbers I can create different 4x4 matrices. Id like to find all 4x4 matrices where each element in the list is used once, and where the sum of each …

How can I access tablet pen data via Python?

I need to access a windows tablet pen data (such as the surface) via Python. I mainly need the position, pressure, and tilt values.I know how to access the Wacom pen data but the windows pen is differe…

Read Celery configuration from Python properties file

I have an application that needs to initialize Celery and other things (e.g. database). I would like to have a .ini file that would contain the applications configuration. This should be passed to th…

numpys tostring/fromstring --- what do I need to specify to restore the array

Given a raw binary representation of a numpy array, what is the complete set of metadata needed to unambiguously restore the array? For example, >>> np.fromstring( np.array([42]).tostring())…

How to limit width of column headers in Pandas

How can I limit the column width within Pandas when displaying dataframes, etc? I know about display.max_colwidth but it doesnt affect column names. Also, I do not want to break the names up, but rath…

Django + Auth0 JWT authentication refusing to decode

I am trying to implement Auth0 JWT-based authentication in my Django REST API using the django-rest-framework. I know that there is a JWT library available for the REST framework, and I have tried usin…

How to measure the angle between 2 lines in a same image using python opencv?

I have detected a lane boundary line which is not straight using hough transform and then extracted that line separately. Then blended with another image that has a straight line. Now I need to calcula…

How to modify variables in another python file?

windows 10 - python 3.5.2Hi, I have the two following python files, and I want to edit the second files variables using the code in the first python file.firstfile.pyfrom X.secondfile import *def edit(…