Skip to content

Instantly share code, notes, and snippets.

@hygull
Last active November 22, 2018 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hygull/cc5a7f093d71e688fb04cf56ba22d368 to your computer and use it in GitHub Desktop.
Save hygull/cc5a7f093d71e688fb04cf56ba22d368 to your computer and use it in GitHub Desktop.
Pandas, stackoverflow, df.merge(), df.index.name

https://stackoverflow.com/questions/53405278/how-to-merge-different-rows-in-a-csv-files-by-python/53405632?noredirect=1#comment93719012_53405632

>>> import pandas as pd
>>>
>>> dri = pd.date_range("2018/01/01", periods=2, freq="d")
>>>
>>> df = pd.DataFrame({"time": dri, "price": [12, 15]}, index = [1, 2])
>>> df
        time  price
1 2018-01-01     12
2 2018-01-02     15
>>>
>>> df2 = pd.DataFrame({"time": dri, "address": ["MI", "AR"]}, index=[1, 2])
>>> df2
        time address
1 2018-01-01      MI
2 2018-01-02      AR
>>>
>>> # https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html
...
>>>
>>> df.merge(df2, on = "time", how = "inner", left_index = True)
        time  price address
1 2018-01-01     12      MI
2 2018-01-02     15      AR
>>>


>>> df.index.name = "row number"
>>> df
                 time  price
row number
1          2018-01-01     12
2          2018-01-02     15
>>>
>>> df2.index.name = "row number"
>>>
>>> df2
                 time address
row number
1          2018-01-01      MI
2          2018-01-02      AR
>>>
>>> df.merge(df2, on = "time", how = "inner", left_index = True)
                 time  price address
row number
1          2018-01-01     12      MI
2          2018-01-02     15      AR
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment