subtract each row from all other rows and view as matrix in python

nay

I have df like this:

id   date
1    01-02-2013
2    01-06-2013
3    05-31-2013
4    07-06-2013

and I want to build a matrix that shows me for each id, the time elapsed between the specific id to all others ( in days). i.e. something like this:

   1  2  3    4
1  0  4 -149  -185
2  4  0  -145  -181
....

Thx

Don'tAccept
df['date'] = pd.to_datetime(df['date'])
df.set_index('id', inplace=True)

You can just subtract all the values of date column from each value in it, End result is:

df['date'].apply(lambda x:x-df['date'])
id        1        2         3         4
id                                      
1    0 days  -4 days -149 days -185 days
2    4 days   0 days -145 days -181 days
3  149 days 145 days    0 days  -36 days
4  185 days 181 days   36 days    0 days

And if you don't want to display days string, you can use dt.days attribute to access the number of days:

df['date'].apply(lambda x:x-df['date']).apply(lambda x: x.dt.days)
id    1    2    3    4
id                    
1     0   -4 -149 -185
2     4    0 -145 -181
3   149  145    0  -36
4   185  181   36    0

You can finally use .values attribute if you want to get numpy array:

df['date'].apply(lambda x:x-df['date']).apply(lambda x: x.dt.days).values

array([[   0,   -4, -149, -185],
       [   4,    0, -145, -181],
       [ 149,  145,    0,  -36],
       [ 185,  181,   36,    0]], dtype=int64)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python subtract first element from each respective row of matrix

subtract the values from one matrix by row for each column of another matrix

Subtract the value in a field in one row from all other rows of the same field in pandas dataframe

Python: Store indices of non-zero unique rows after comparing each rows with every other row in a matrix

Subtract the Blank row from the other rows but only from specific columns

MSSQL Subtract two rows from each other in union

R: Subtract rows from the first row in each group

How to add/subtract row values from each other in pandas?

how to multiply each row from one matrix to every rows to another matrix on Python?

Subtract each row of matrix A from every row of matrix B without loops

Subtract row from all rows with same identifier using R

How to compare each row from one dataframe against all the rows from other dataframe and calculate distance measure?

Python Dataframe subtract a value from each list of a row

Subtract each row of specific column from dataframe and add to the list -python

comparing each row with all other rows in data.frame

Subtract all items in a list against each other

Filtering rows from Matrix based on values of all cells of the row

Subtract first row in a column from itself and all rows in that column, for all columns but one, in R

SQL Subtract two rows from each other in same column to get a result

how to generate a new rows and adding 1 min after subtract two datetimes from each other

subtract a value from all the elements of matrix in openCV

Datetime in pandas dataframe will not subtract from each other

Subtract two strings from each other

subtract first or second value from each row

Replace all the div from row without moving other rows of a grid

How can I subtract values of each column of my dataframe from all other columns?

For each day, subtract last time row from the first time row with Python

Multiplication of matrix rows with each other and with another list

Python Pandas merging two data frames and map one row from one data frame to all rows from the other