Answer the question
In order to leave comments, you need to log in
Why does pandas datetime index change to normal when merging two dataframes using .merge?
Good afternoon.
Tell me, why when merging two Left Join dataframes (one of them with a datetime index, the other with regular numeric ones) using .merge in pandas, the datetime index in the resulting dataframe is not saved, but ordinary ones come out?
Code:
in
cards = pd.read_csv('/content/drive/My Drive/for study/card_data.csv', encoding = 'cp1250') # load the file cards
cards = cards[::100] # select 100 elements for using less memory
cards.head()
out
CustomerID TransactionNum
0 17850.0 127
100 14688.0 248
200 12431.0 241
300 13448.0 356
400 16218.0 212
in
data = pd.read_csv('/content/drive/My Drive/study/data.csv', encoding = 'cp1250') # load cards file
data['TotalCost'] = data['Quantity'] * data[' UnitPrice'] # and do all the same steps from the first task, create a new TotalCost column as the product of Quantity * UnitPrice
data.index = pd.to_datetime(data.InvoiceDate) # turn the InvoiceDate column into an index
data.drop('InvoiceDate', axis =1, inplace=True) # remove InvoiceDate
data = data[::100] # allocate 100 items to use less memory
data.head()
out
InvoiceNo StockCode Quantity UnitPrice CustomerID Country TotalCost
InvoiceDate
2010-12-01 08:26:00 536365 85123A 6 2.55 17850.0 United Kingdom 15.3
2010-12-01 09:37:00 536378 84519A 6 2.95 14688.0 United Kingdom 17.7
2010-12-01 10:03:00 536389 35004c 12431.0 Australia 32.7
2010-12-01 10:52:00 536398 22468 4 6.75 13448.0 United Kingdom 27.0
2010-12-01 11:29:00 536404 21061 12 0.85 16218.0
United
Kingdom 'CustomerID', how='left') # Left Join data and cards table on common column CustomerID
new_data.head()
out
InvoiceNo StockCode Quantity UnitPrice CustomerID Country TotalCost TransactionNum
0 536365 85123A 6 2.55 17850.0 United Kingdom 15.3 127
1 536365 85123A 6 2.55 17850.0 United Kingdom 15.3
2 536365 85123A 6 2.55 17850.0 United Kingdom 15.3 226
3 536365 85123A 6 2.55 17850.0 United Kingdom
17
Answer the question
In order to leave comments, you need to log in
Try playing around with index parameters
pd.merge(df1, df2, left_index=True, right_index=True)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question