0

I have three dataframes. I want to select the row by date 2020-10-26 from all 3 dataframes and than I want to create a new dataframe. How to do it?

import pandas as pd
from datetime import date

date1 = date.fromisoformat('2020-10-25')
date2 = date.fromisoformat('2020-10-26')
date3 = date.fromisoformat('2020-10-27')

for x in range(3):
    df = pd.DataFrame([(date1,f'Stu{x}j', x+1, f'Varan{x}j', x*400+2), 
                        (date2,f'aja{x}k', x+2, f'Del{x}j', x*634+3), 
                        (date3,f'Aadi{x}t', x+4, f'Mumb{x}j', x*454+4),
                        (date2,f'har{x}h', x+5, f'bom{x}j', x*124+5)],
                       columns =['Date','Name', 'Age',  
                             'City', 'Salary'])
    df.set_index('Date', inplace = True)
    print(df)


1
  • 1
    What is your expected output? How to deal with conflicting contents at the same date and the same column name(s)? Commented Oct 28, 2020 at 13:15

2 Answers 2

1

Below code would help you

import pandas as pd
from datetime import date

date1 = date.fromisoformat('2020-10-25')
date2 = date.fromisoformat('2020-10-26')
date3 = date.fromisoformat('2020-10-27')
res = pd.DataFrame()
for x in range(3):
    df = pd.DataFrame([(date1,f'Stu{x}j', x+1, f'Varan{x}j', x*400+2), 
                        (date2,f'aja{x}k', x+2, f'Del{x}j', x*634+3), 
                        (date3,f'Aadi{x}t', x+4, f'Mumb{x}j', x*454+4),
                        (date2,f'har{x}h', x+5, f'bom{x}j', x*124+5)],
                       columns =['Date','Name', 'Age',  
                             'City', 'Salary'])
    temp = df[df['Date'].isin([date2])]
    res = res.append(temp)
print(res)

Old method:

fr = [df1, df2, df3]
full = pd.concat(fr).reset_index()
full[full.Date.isin([date2])]
Sign up to request clarification or add additional context in comments.

5 Comments

error ObjectHashTable.get_item KeyError: 'Date' @SubbuVidyaSekar
yes but my real code is different then this dummy code I think your previous solution was correct please write the last two step after combining all df @SubbuVidyaSekar
Added the old method in the answer @stackdotpop
still not working if we select row and append it in empty df in every iteration I think it will work what do you think??@SubbuVidyaSekar
0

You can do:

row1 = df1.loc[date2, :]
row2 = df2.loc[date2, :]
row3 = df3.loc[date2, :]
pd.concat([row1,row2,row3], axis=1).T

Output:

            Name    Age City        Salary
2020-10-26  ajay    32  Delhi       25000
2020-10-26  Saumya  32  Kanpur      20000
2020-10-26  Aaditya 40  Dehradun    24000

1 Comment

How to achive this on a loop I have changed my code please visit again Thankyou @Marcus

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.