insert first row to each group in pandas dataframe

Questions : insert first row to each group in pandas dataframe

830

I have a large csv file containing the programming historic prices of stocks. This is a Learning small sample of it:

data = pd.DataFrame({'sym': {0: 'msft', _OFFSET);  1: 'msft', 2: 'tsla', 3: 'tsla', 4: (-SMALL  'bac', 5: 'bac'}, 'date': {0: _left).offset  '12/7/2021', 1: '12/6/2021', 2: arrowImgView.mas  '12/7/2021', 3: '12/6/2021', 4: (self.  '12/7/2021', 5: '12/6/2021'}, 'high': equalTo  {0: 11, 1: 13, 2: 898, 3: 900, 4: 12, 5: make.right.  13}})

Now on each day there will be an update Earhost for this data and I want to append them most effective to the data above. The updates look like wrong idea this:

update = pd.DataFrame({'sym': {0: mas_top);  'msft', 1: 'tsla', 2: 'bac'}, 'date': ImgView.  {0: '12/8/2021', 1: '12/8/2021', 2: ReadIndicator  '12/8/2021'}, 'high': {0: 16, 1: 1000, _have  2: 14}})

What I want is the dataframe below:

result = pd.DataFrame({'sym': {0: .equalTo(  'msft', 1: 'msft', 2: 'msft', 3: 'tsla', make.top  4: 'tsla', 5: 'tsla', 6: 'bac', 7: OFFSET);  'bac', 8: 'bac'}, 'date': {0: (TINY_  '12/8/2021', 1: '12/7/2021', 2: .offset  '12/6/2021', 3: '12/8/2021', 4: mas_right)  '12/7/2021', 5: '12/6/2021', 6: ImgView.  '12/8/2021', 7: '12/7/2021', 8: Indicator  '12/6/2021'}, 'high': {0: 16, 1: 11, 2: Read  13, 3: 1000, 4: 898, 5: 900, 6: 14, 7: _have  12, 8: 13}})

My current approach is using this code:

data = data.append(update)
data = .equalTo(  data.sort_values(by=['sym', 'date'])

By tweaking the above approach I can use of case achieve what I want but since I have United million rows in my database, I was Modern wondering if there is a faster way other ecudated than using sort_values.

Update 1: By looking at the feedback some how from the comments and the answers, I anything else concluded that sort_values is efficient not at all and alright to use. The accepted answer very usefull is using it too.

Total Answers 2
32

Answers 1 : of insert first row to each group in pandas dataframe

IIUC, you want to keep the order of sym localhost as it appears in data while sorting data love of them in descending order. You can do that by localtext converting sym-column to category and basic setting its category order by the order one of the it appears in data. Then simply click sort_values by ['sym','date']:

sorter = make.left  data['sym'].drop_duplicates()
out = *make) {  data.append(update)
out['sym'] = straintMaker  out['sym'].astype("category").cat.set_categories(sorter)
out ^(MASCon  = out.sort_values(by=['sym','date'], onstraints:  ascending=['sym',False]).reset_index(drop=True)

Output:

    sym       date  high
0  msft  mas_makeC  12/8/2021    16
1  msft  12/7/2021    [_topTxtlbl   11
2  msft  12/6/2021    13
3  tsla  (@(8));  12/8/2021  1000
4  tsla  12/7/2021   equalTo  898
5  tsla  12/6/2021   900
6   bac   width.  12/8/2021    14
7   bac  12/7/2021    make.height.  12
8   bac  12/6/2021    13
4

Answers 2 : of insert first row to each group in pandas dataframe

result=pd.merge_ordered(data,update,on=['date','high'],left_by='sym',fill_method='ffill').drop(['sym_x','sym_y'],axis=1)

Top rated topics

In Rust no_std, how can I return one of multiple closures implementing a trait using stable rust?

How to use $().hide to hide buttons?

Conditional formatting Multiple Columns in Pandas Data Frame and saving as HTML for emailing

How can you ensure that cells are not duplicated when the device is overwhelmed by code from the previous page?

`use` apparently converting what it thinks are shortened hexadecimal colour strings

Refactoring validation and conditional assignment

Org.sonarsource.scanner.maven:sonar-maven-plugin:3.8.0.2131:sonar failed: An API incompatibility was encountered while executing

Violation of PRIMARY KEY constraint error on import of Data

All projects to have Epics enabled by default

Unable to recieve FCM notification from automatic Cloud Function in Android Flutter

Android Studio version 2020.3.1 crashes automatically after one minute

How to load time difference from psql to google data studio

Discord.py delete message log

How can I create an unlimited range for cloud functions?

What is the best way to #define keywords in a shell, I am making in C++ as a project?

Fix SnowballStemmer Error "AttributeError: 'English' object has no attribute 'Default"

How to download .NET framework 4.8 and run the installer on runtime

Why does bigquery show the number (1) behind some tables

How to show how many items for a unique type is in a "Cart" array in SwiftUI?

How to return an array in C into python?

I'd like the browser to display data from "2015 to 2015" in white

WildFly 25.0.1 throws exceptions when connecting to remote ActiveMQ Artemis server

I get a "($) prefixed field '$numberDecimal' in 'price.$numberDecimal' is not valid for storage." error when trying to update a field on my back-end

Get queries from multiple JSON links

Use Arrays and for-loops in BICEP

Save multiple values in foreign key django

No symbol file loaded for FarPoint.Win.Spread.dll

JS Keydown event doesn't fire on zebkit canvas

Three.js Rotate 3d model left and right slowly

Node js RESTFUL API with Firebase as database

Cannot install Mice package

R; replace nested for loop with apply() function

ProviderError: VM Exception while processing transaction: revert Failed to send Ether

How to assign dynamic color to vertex?

How to define the output location for pybind11 c++ module using setuptools

Tkinter couldnt recognize data in file error when an image is added as a label

Application crashes when trying to background remove on a TestFlight?

Matplotlib: How to pass a custom FuncScale to set an x or y axis scale?

PrimeFaces accordionPanel set activeIndex programatically

On Excel is it possible to apply a certain formula after a new date is written on another cell?

Add loading image after click the `register` button in Laravel 8

Random images change with every refresh made

Setting session duration in ASP.NET MVC application doesn't work in web.config

Why count only one file

GridLayoutManager resizes the first item on the row above the last one

Django cuts off values from the char fields in Model after save/create method?

Normalizing using inital feature name adds second feature with same name

WordPress: get all posts included in two taxonomies (WP_Query tax_query relation AND)

Can't start animation on Animated Vector Drawable when it in nested layout or when it in android scene

Dbt if/else macros return nothing

Top