python looping and creating new dataframe for each value of a column

2024/10/14 9:27:08

I want to create a new dataframe for each unique value of station.

I tried below which gives me only last station data updated in the dataframe = tai_new.i

tai['station'].unique() has 500 values.for i in tai['station'].unique():tai_new.i = tai[tai_2['station'] ==i]

Another approach is creating a separate list of

tai_stations = tai['station'].unique()

And then create two loops however, I do not want to type 500 (IF) conditions.

Answer

You can create dict of DataFrames by convert groupby object to tuples and then to dict:

dfs = dict(tuple(tai.groupby('station')))

Sample:

tai = pd.DataFrame({'A':list('abcdef'),'B':[4,5,4,5,5,4],'C':[7,8,9,4,2,3],'D':[1,3,5,7,1,0],'E':[5,3,6,9,2,4],'station':list('aabbcc')})print (tai)A  B  C  D  E station
0  a  4  7  1  5       a
1  b  5  8  3  3       a
2  c  4  9  5  6       b
3  d  5  4  7  9       b
4  e  5  2  1  2       c
5  f  4  3  0  4       cdfs = dict(tuple(tai.groupby('station')))#select each DataFrame by key - name of station
print (dfs['a'])A  B  C  D  E station
0  a  4  7  1  5       a
1  b  5  8  3  3       aprint (type(dfs['a']))
<class 'pandas.core.frame.DataFrame'>
https://en.xdnf.cn/q/69429.html

Related Q&A

How to put more whitespace around my plots?

I have a figure that contains two subplots in two rows and one column like so:fig, (ax1, ax2) = subplots(nrows=2,ncols=1, )The two subplots are pie charts, therefore I want their axes to be square. Aft…

using ols from statsmodels.formula.api - how to remove constant term?

Im following this first example in statsmodels tutorial:http://statsmodels.sourceforge.net/devel/How do I specify not to use constant term for linear fit in ols?# Fit regression model (using the natur…

Is numerical encoding necessary for the target variable in classification?

I am using sklearn for text classification, all my features are numerical but my target variable labels are in text. I can understand the rationale behind encoding features to numerics but dont think t…

django - regex for optional url parameters

I have a view in django that can accept a number of different filter parameters, but they are all optional. If I have 6 optional filters, do I really have to write urls for every combination of the 6 …

How do I remove transparency from a histogram created using Seaborn in python?

Im creating histograms using seaborn in python and want to customize the colors. The default settings create transparent histograms, and I would like mine to be solid. How do I remove the transparency?…

Set confidence levels in seaborn kdeplot

Im completely new to seaborn, so apologies if this is a simple question, but I cannot find anywhere in the documentation a description of how the levels plotted by n_levels are controlled in kdeplot. T…

OpenCV (cv2 in Python) VideoCapture not releasing camera after deletion

I am relatively new to Python, just having learnt it over the past month or so and have hacked this together based off examples and others code I found online.I have gotten a Tkinter GUI to display the…

Paho MQTT Python Client: No exceptions thrown, just stops

I try to setup a mqtt client in python3. This is not the first time im doing this, however i came across a rather odd behaviour. When trying to call a function, which contains a bug, from one of the c…

SSH Key-Forwarding using python paramiko

We currently run a script on our desktop that uses paramiko to ssh to a remote linux host. Once we are on the remote linux host we execute another command to log into another remote machine. What we wa…

Is it possible to ignore Matplotlib first default color for plotting?

Matplotlib plots each column of my matrix a with 4 columns by blue, yellow, green, red.Then, I plot only the second, third, fourth columns from matrix a[:,1:4]. Is it possible to make Matplotlib ignore…