logo
Tags down

shadow

Construct new column with first row of a groupby with two columns - Pandas


By : Siwo
Date : October 17 2020, 06:10 AM
Any of those help I am trying to construct a new column that gives a value of 1 if it's the first time that an element of the column "type" has had a specific value of the column "xx", and gives a value of 0 in any other case. , Try:
code :
df['yy'] = (df.groupby(level=1).xx
              .apply(lambda x: (~x.duplicated()).astype(int))
           )

df['yy']
date        type
2018-01-01  A       1
            B       1
2018-02-01  B       1
2018-03-01  A       1
            B       1
            C       1
2018-04-01  A       0
            B       0
2018-05-01  B       0
2018-06-01  A       1
            B       0
            C       1
2018-07-01  A       0
2018-08-01  B       0
            C       1
2018-09-01  A       1
            B       1
2018-10-01  C       0
            A       0
            B       0
2018-11-01  A       0
2018-12-01  B       0
            C       1
Name: yy, dtype: int32


Share : facebook icon twitter icon

Pandas groupby 2 columns, select max of 3rd column


By : Handsome Youngman
Date : March 29 2020, 07:55 AM
Hope that helps It seems to me that you actually just want the max shape area for each FID_preproc:
code :
In[34]:
maxarea = df.loc[df.groupby('FID_preproc', as_index=False)['Shape_Area'].idxmax()]
maxarea

Out[34]: 
   FID_preproc NAME    Shape_Area
0         1340    A   25952.35775
2         1341    B  305220.12440
3         1342    A   12053.13585

Groupby one column and top N from other columns pandas


By : user7374094
Date : March 29 2020, 07:55 AM
Any of those help You can use sort_values + groupby + GroupBy.head:
code :
df = df.sort_values(['main_id','Scores'], ascending=[True,False]).groupby('main_id').head(2)
print (df)
   main_id b_code  Scores
4        1    ZXC    0.80
2        1    BCD    0.70
8        2    QWE    0.87
6        2    ABC    0.81
df = df.set_index('b_code').groupby('main_id')['Scores'].nlargest(2).reset_index()
print (df)
   main_id b_code  Scores
0        1    ZXC    0.80
1        1    BCD    0.70
2        2    QWE    0.87
3        2    ABC    0.81

groupby and sum two columns and set as one column in pandas


By : baejoseph
Date : March 29 2020, 07:55 AM
seems to work fine I have the following data frame: , A simple way is to create two new Series indexed by the teams:
code :
home = pd.Series(data.HomePoint.values, data.Home)
away = pd.Series(data.AwayPoint.values, data.Away)
home.add(away, fill_value=0).astype(int)
goo = data.melt(['HomePoint', 'AwayPoint'], var_name='At', value_name='Team')
goo.HomePoint.where(goo.At == 'Home', goo.AwayPoint).groupby(goo.Team).sum()
ooze = data.melt(['Home', 'Away'])
ooze.value.groupby(ooze.Home.where(ooze.variable == 'HomePoint', ooze.Away)).sum()

Keep One Column but Using Other Columns in Pandas Groupby and Agg


By : Iqbal Hussain
Date : March 29 2020, 07:55 AM
I hope this helps . I have a goal of grouping a dataset by certain column (identifier) and then perform some customized operations (first sort by date, and then concatenate the status). , IIUC, you can slice first and then just agg
code :
df['letter'] = df.status.str[0]
df.groupby('identifier').letter.agg(''.join)

identifier
A     PA
B    PPA
C    PPP
def myfunc(ser):
    res = [s[0] for s in ser]
    return ''.join(res)

df = df.sort_values('date', ascending=True)
df.groupby(['identifier']).agg({'status': lambda x: myfunc(x)})
df.groupby(['identifier']).status.agg(lambda x: myfunc(x))

#same as 
df.groupby(['identifier']).status.agg(myfunc) 

pandas groupby column then create two other columns based on third column


By : user2137969
Date : March 29 2020, 07:55 AM
Hope that helps I am trying to group a dataframe by a column and get the total "Approved" or "Not Approved" values based on this grouping, but with no success. , Given
code :
>>> df
   PlaceTest  Approved
0         21         1
1         21         0
2         22         1
3         22         0
>>> df.assign(NotApproved=1 - df['Approved']).groupby('PlaceTest').sum().reset_index()
   PlaceTest  Approved  NotApproved
0         21         1            1
1         22         1            1
Related Posts Related Posts :
  • How do I acess my Spider data from my main.py script?
  • Python Pandas Expand a Column of List of Lists to Two New Column
  • Overhead of python multiprocessing initialization is worse than benefits
  • Python Joining List and adding and removing characters
  • Adding an lxml library to project
  • Concatenating tensors in Tensorflow with None axis
  • Need help understanding why i get attribute error
  • How to force a MIDI device to report control status?
  • What does *** mean in Python -3?
  • How to get GFCC instead of MFCC in python?
  • How do I print a number n times in python?
  • How do i split a string wherever there are digits?
  • List Comprehension Python Prime numbers
  • "list index out of range" when reading data from file
  • What's the correct datetime format for the specified date string?
  • I cannot import CSV file?
  • Matplotlib pyplot plots look different after calling pandas profiling. How can I fix this?
  • Stopping all the instances of a specific region
  • Deal with Birtish summer time
  • Unable to use ColorWheel without loading kv (AttributeError)
  • What are these characters called: 。. !?etc Trying to split sentences stops working with non standard characters
  • rand.randint returning same number over and over?
  • Find longest sequence that does not contain a certain number
  • How do I convert a map object to list and also assign to a variable
  • sympy error: 'Symbol' object has no attribute 'pi'
  • How to remove words without vowels from a list in python
  • Downloading python to macbook
  • TypeError: __init__() missing 1 required positional argument: 'units'
  • Check if a class is a dataclass in Python
  • Unable to scrape google news heading via their class
  • Array of structs with dynamic allocation runs very slow in C in comparison to Python
  • Python Pandas - find all unique combinations of rows of a DataFrame without repeating values in the columns
  • How do I change the numbers in a cell to the word 'Bus' in Pandas Python
  • 'ascii' codec can't encode character : ordinal not in range (128)
  • How to split an array of arrays?
  • AttributeError: 'str' object has no attribute 'courseGrade'
  • Run a perl script with Python on multiple files at once in a folder
  • glob() got an unexpected keyword argument 'recursive'
  • Python .sort() does not fully order numbers
  • Getting indexes of all max values in a list
  • How to handle threads in Python functions in Google Cloud Functions?
  • Comparing list elements of two lists (with a twist)
  • Scrapy problems with requests
  • Python go back through list of lists to see if value exists
  • Keras Unet + VGG16 predictions are all the same
  • Python dictionary is splitting string keys by character
  • Pandas: get rid off rows where there is no value
  • how to extract URI from bunch of URLs?
  • How to merge first 12 columns?
  • How to scrape movies information from the IMDB website?
  • I am having problems with import nmap in pycharm
  • How to deploy rabbitmq with flask and docker?
  • Python: "or" in short form
  • Python Django - "AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL
  • From a list of datetime timestamps across multiple days, how to find the last time stamp across each day?
  • I don't understand the order of methods in python
  • How to repeat pandas dataframe records based on column value
  • Pandas merge by name and date (multiple columns)
  • PRAW bot only parsing one subreddit when wanted to parse multiple
  • Error using %s for String Formatting in MySQL Query with Python
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk