logo
down
shadow

Get the earliest date from a column (Python Pandas) after csv.reader


Get the earliest date from a column (Python Pandas) after csv.reader

Content Index :

Get the earliest date from a column (Python Pandas) after csv.reader
Tag : python , By : eferro
Date : November 27 2020, 03:01 PM

With these it helps I think you need boolean indexing for filtering:
#dont filter all columns by usecols    
df = pd.read_csv('file', parse_dates=['Start Date', 'End Date']) #columns to datetimes

#filter output first by column ID and then get min and max
a = df.loc[ df['ID'] == 56886, 'Start Date'].min()


b = df.loc[ df['ID'] == 56886, 'End Date'].max()

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Excel formula for finding second earliest to the earliest date in a column


Tag : excel , By : MP.
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I have a data query in Excel that returns multiple rows of information for a single employee. The employee are sorted by an EmployeeID, and the data contains Pay Rates and Dates. , Instead of VLOOKUP use INDEX/MATCH!
Say your current formula is:
=VLOOKUP(A1,Sheet1!A:C,3,0)
=INDEX(Sheet1!C:C,MATCH(A1,Sheet1!A:A,0))
=INDEX(Sheet1!C:C,MATCH(A1,Sheet1!A:A,0)+1)
=IF(INDEX(Sheet1!B:B,MATCH(A1,Sheet1!A:A,0))=INDEX(Sheet1!B:B,MATCH(A1,Sheet1!A:A,0)+1),INDEX(Sheet1!B:B,MATCH(A1,Sheet1!A:A,0)+1),"Only one entry!")

Python Pandas: how to take only the earliest date in each group


Tag : python , By : Doug
Date : March 29 2020, 07:55 AM
This might help you I'd do this with an optional sort_values call and a drop_duplicates call.
data_all.sort_values(data_all.columns.tolist())\
          .drop_duplicates(subset=['CB date'], keep='first')
  code ticket #    CB date audit date
0    1      303 2016-03-07 2016-02-05
2    1      303 2017-05-06 2016-02-05
4    4      404 2011-06-23 2011-06-23

Pandas group, aggregate two columns and return the earliest Start Date for one column


Tag : python , By : Pierre LeBoo
Date : March 29 2020, 07:55 AM
help you fix your problem I think you need transform:
df = pd.read_csv('sampleBionic.csv')
print (df)
      ID A or B  Start Date    End Date  Value  Site  Value2 Random
0  45221   AAAA  12/30/2017  09/30/2017     14  S111       7      Y
1  45221   AAAA  01/15/2017  09/30/2017     15  S222       7      Y
2  85293   BBBB  05/12/2017  07/24/2017     29  S111       3      Y
3  85293   AAAA  03/22/2017  10/14/2017     32  S222       4      Y
4  45221   AAAA  01/15/2017  09/30/2017     30  S222       7      Y

groupedBy = (df[df['A or B'].str.contains('AAAA')]
                            .groupby([df['ID'], df['Site'].fillna('Other'),])
                            .agg({'Start Date': 'min', 'End Date': 'max', 'Value': 'sum'}))
print (groupedBy)    
            Start Date    End Date  Value
ID    Site                               
45221 S111  12/30/2017  09/30/2017     14
      S222  01/15/2017  09/30/2017     45
85293 S222  03/22/2017  10/14/2017     32

g = groupedBy.groupby(level=0)              
groupedBy['Start Date'] = g['Start Date'].transform('min') 
groupedBy['End Date'] = g['End Date'].transform('max')
print (groupedBy)
            Start Date    End Date  Value
ID    Site                               
45221 S111  01/15/2017  09/30/2017     14
      S222  01/15/2017  09/30/2017     45
85293 S222  03/22/2017  10/14/2017     32

Pandas groupby aggregation to truncate earliest date instead of oldest date


Tag : python , By : flesk
Date : March 29 2020, 07:55 AM
it fixes the issue It seems like the grouper function build up the bins starting from the oldest time in the series that you pass to it. I couldn't see a way to make it build up the bins from the newest time, but it's fairly easy to construct the bins from scratch.
freq = '3min'

minTime = df.date.min()
maxTime = df.date.max()
deltaT = pd.Timedelta(freq)
minTime -= deltaT - (maxTime - minTime) % deltaT # adjust min time to start of first bin
r = pd.date_range(start=minTime, end=maxTime, freq=freq)

df.groupby(pd.cut(df["date"], r)).agg('count')
date                                     date number        
(1999-12-31 23:58:00, 2000-01-01 00:01:00]  2   2
(2000-01-01 00:01:00, 2000-01-01 00:04:00]  3   3
(2000-01-01 00:04:00, 2000-01-01 00:07:00]  3   3

Python Pandas - New Column Returns earliest data for each unique ID


Tag : python-3.x , By : enginecrew
Date : March 29 2020, 07:55 AM
I hope this helps you . I have a data set with three columns (Customer ID, transaction, date). There is a one to many relationship between Customer ID and transaction. I want to add in a new column that contains the earliest purchase data for each unique Customer ID. I tried the code below , here's the code you need
min_dates = df.groupby(['Customer ID'])['Date'].min()
df['First Purchase Date'] = df.apply(lambda row: min_dates.loc[row['Customer ID']], axis=1)
csv = """Customer ID,Date
1,2019
1,2018
1,2020
2,2000
2,2010
2,2005"""
from io import StringIO
import pandas as pd
df = pd.read_csv(StringIO(csv))

min_dates = df.groupby(['Customer ID'])['Date'].min()
df['First Purchase Date'] = df.apply(lambda row: min_dates.loc[row['Customer ID']], axis=1)
print(df)
    Customer ID Date    First Purchase Date
0   1           2019    2018
1   1           2018    2018
2   1           2020    2018
3   2           2000    2000
4   2           2010    2000
5   2           2005    2000
Related Posts Related QUESTIONS :
  • Open .h5 file in Python
  • Joining a directory name with a binary file name
  • python, sort list with two arguments in compare function
  • Is it possible to print from Python using non-ANSI colors?
  • Pandas concat historical data using date minus some number of days
  • CV2: Import Error in Python OpenCV
  • Is it possible to do this loop in a one-liner?
  • invalid literal for int() with base 10: - django
  • Why does my code print a value that I have not assigned as yet?
  • the collatz func in automate boring stuff with python
  • How to find all possible combinations of parameters and funtions
  • about backpropagation deep neural network in tensorflow
  • Sort strings in pandas
  • How do access my flask app hosted in docker?
  • Replace the sentence include some text with Python regex
  • Counting the most common element in a 2D List in Python
  • logout a user from the system using a function in python
  • mp4 metadata not found but exists
  • Django: QuerySet with ExpressionWrapper
  • Pandas string search in list of dicts
  • Decryption from RSA encrypted string from sqlite is not the same
  • need of maximum value in int
  • a list of several tuples, how to extract the same of the first two elements in the small tuple in the large tuple
  • Display image of 2D Sinewaves in 3D
  • how to prevent a for loop from overwriting a dictionary?
  • How To Fix: RuntimeError: size mismatch in pyTorch
  • Concatenating two Pandas DataFrames while maintaining index order
  • Why does this not run into an infinite loop?
  • Python Multithreading no current event loop
  • Element Tree - Seaching for specific element value without looping
  • Ignore Nulls in pandas map dictionary
  • How do I get scrap data from web pages using beautifulsoup in python
  • Variable used, golobal or local?
  • I have a regex statement to pull all numbers out of a text file, but it only finds 77 out of the 81 numbers in the file
  • How do I create a dataframe of jobs and companies that includes hyperlinks?
  • Detect if user has clicked the 'maximized' button
  • Does flask_login automatically set the "next" argument?
  • Indents in python 3
  • How to create a pool of threads
  • Pandas giving IndexError on one dataframe but not on another similar dataframe
  • Django Rest Framework - Testing client.login doesn't login user, ret anonymous user
  • Running dag without dag file in airflow
  • Filling across a specified dimension of a numpy array
  • Python populating dataframe in pandas from text files
  • How to interpolate a single ("non-piecewise") cubic spline from a set of data points?
  • Divide 2 integers (leetcode 29) - recursion issue
  • Can someone explain why do I get this output in Python?
  • How do I scrape pdf and html from search results without obvious url
  • Is there a way to automatically make a "collage" of plots with matplotlib?
  • How to combine multiple rows in pandas with shared column values
  • How do I get LOAD_CLASSDEREF instruction after dis.dis?
  • Django - How to add items to Bootstrap dropdown?
  • Linear Regression - Does the below implementation of ridge regression finding coefficient term using gradient method is
  • How to drop all rows in pandas dataframe with negative values?
  • Most Efficient Way to Find Closest Date Between 2 Dataframes
  • Execution error when Passing arguments to a python script using os.system. The script takes sys.argv arguments
  • Looping through a function
  • Create a plot for each unique ID
  • a thread python with 'while' got another thread never start
  • Solution from SciPy solve_ivp contains oscillations for a system of first-order ODEs
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com