Home>
import pandas as pd
import numpy as np
one = pd.read_csv ('data1.csv')
two = pd.read_csv ('data2.csv')
I wrote

and the code.
one is

A Date
10 2011-07-03
20 2011-07-04
10 2011-07-06
20 2011-07-07
30 2011-07-10
40 2011-07-13
25 2011-07-15

two is

B Date
15 2011-07-01
15 2011-07-02
15 2011-07-03
25 2011-07-07
35 2011-07-10
10 2011-07-13
25 2011-07-15
It is a DataFrame like

.
I want to make only one and two DataFrame data that is common to both Dates.
For example, if i am one,

A Date
10 2011-07-03
20 2011-07-07
30 2011-07-10
40 2011-07-13
25 2011-07-15


If i am two,

B Date
15 2011-07-03
25 2011-07-07
35 2011-07-10
10 2011-07-13
25 2011-07-15


I want to How can I take the common term of two DataFrame columns and transform the DataFrame?

  • Answer # 1

    oneandtwoshould be handled as a single DataFrame bymerge ()in'Date'Column Is it not?

    one_and_two = pd.merge (one, two, on = 'Date', how = 'inner')
    print (one_and_two)
    # A Date B
    # 0 10 2011-07-03 15
    # 1 20 2011-07-07 25
    # 2 30 2011-07-10 35
    # 3 40 2011-07-13 10
    # 4 25 2011-07-15 25

    If you really want to handleoneandtwoin different DataFrames,

    one = one_and_two [['A', 'Date']]
    two = one_and_two [['B', 'Date']]


    To split it