pandas cut multiple columns

2024/10/13 15:26:58

I am looking to apply a bin across a number of columns.

a = [1, 2, 9, 1, 5, 3]
b = [9, 8, 7, 8, 9, 1]c = [a, b]print(pd.cut(c, 3, labels=False))

which works great and creates:

[[0 0 2 0 1 0]
[2 2 2 2 2 0]]

However, i would like to apply the 'cut' to create a dataframe with number and bin it as below.

   Values  bin
0       1    0
1       2    0
2       9    2
3       1    0
4       5    1
5       3    0Values  bin
0       9    2
1       8    2
2       7    2
3       8    2
4       9    2
5       1    0 

This is a simple example of what im looking to do. In reality i 63 separate dataframes and a & b are examples of a column from each dataframe.

Answer

Use zip with a list comp to build a list of dataframes -

c = [a, b]
r = pd.cut(c, 3, labels=False)df_list = [pd.DataFrame({'Values' : v, 'Labels' : l}) for v, l in zip(c, r)]

df_list[   Labels  Values0       0       11       0       22       2       93       0       14       1       55       0       3,    Labels  Values0       2       91       2       82       2       73       2       84       2       95       0       1]
https://en.xdnf.cn/q/69522.html

Related Q&A

Tracking the number of recursive calls without using global variables in Python

How to track the number of recursive calls without using global variables in Python. For example, how to modify the following function to keep track the number of calls?def f(n):if n == 1:return 1else…

Match string in python regardless of upper and lower case differences [duplicate]

This question already has answers here:Case insensitive in(12 answers)Closed 9 years ago.Im trying to find a match value from a keyword using python. My values are stored in a list (my_list) and in the…

Can celery celerybeat use a Database Scheduler without Django?

I have a small infrastructure plan that does not include Django. But, because of my experience with Django, I really like Celery. All I really need is Redis + Celery to make my project. Instead of usin…

Django UserCreationForm custom fields

I am trying to create form for user registration and add some custom fields. For doing that, Ive subclassed UserCretionForm and added fields as shown in django documentation. Then Ive created function-…

Why val_loss and val_acc are not displaying?

When the training starts, in the run window only loss and acc are displayed, the val_loss and val_acc are missing. Only at the end, these values are showed. model.add(Flatten()) model.add(Dense(512, ac…

Is there a python module to solve/integrate a system of stochastic differential equations?

I have a system of stochastic differential equations that I would like to solve. I was hoping that this issue was already address. I am a bit concerned about constructing my own solver because I fear m…

How does thread pooling works, and how to implement it in an async/await env like NodeJS?

I need to run a function int f(int i) with 10_000 parameters and it takes around 1sec to execute due to I/O time. In a language like Python, I can use threads (or async/await, I know, but Ill talk abou…

Calculate centroid of entire GeoDataFrame of points

I would like to import some waypoints/markers from a geojson file. Then determine the centroid of all of the points. My code calculates the centroid of each point not the centroid of all points in the …

Flask-Babel localized strings within js

Im pretty new to both Python and Flask (with Jinja2 as template engine) and I am not sure I am doing it the right way. I am using Flask-Babel extension to add i18n support to my web application. I want…

a (presumably basic) web scraping of http://www.ssa.gov/cgi-bin/popularnames.cgi in urllib

I am very new to Python (and web scraping). Let me ask you a question. Many website actually do not report its specific URLs in Firefox or other browsers. For example, Social Security Admin shows popul…