0

I'm learning about Merge and Join in pandas by reading the documentation. It seems to me that append is just like the concatalong axis = 0. And concathas more keyword arguments. For simple join/concatenation along axis = 0, are append and concat interchangeable?

And I have trouble understanding this sentence in the linked documentation about append:

Note Unlike the append() method, which appends to the original list and returns None, append() here does not modify df1 and returns its copy with df2 appended.

I'm really confused by this sentence. It seems to be comparing append() to append(). which are identical. What did I miss here?

I am not sure if I can put my second question here. If it's against the rules, please let me know or help me edit the question.

Thanks for your time in advance.

Bowen Liu
  • 819
  • 1
  • 7
  • 18
  • 2
    As for your second question, the reference was for the `append `method for `lists`, in contrast to the `append` method for `pd.DaraFrame`s. For `append`vs`concat`, might want to look [here](https://stackoverflow.com/questions/15819050/pandas-dataframe-concat-vs-append) and [here](https://stackoverflow.com/questions/38256104/differences-between-merge-and-concat-in-pandas) – rafaelc Sep 14 '18 at 21:00
  • @RafaelC Oh that certainly cleared things up for me. Thanks. – Bowen Liu Sep 17 '18 at 12:37

1 Answers1

1

Yes, pd.append simply calls pd.concat with the default arguments axis=0, join='outer' which you can see in the return statement. It also has limited functionality, so you can't use it to construct a hierarchical index.

pd.append source

    from pandas.core.reshape.concat import concat
    if isinstance(other, (list, tuple)):
        to_concat = [self] + other
    else:
        to_concat = [self, other]
    return concat(to_concat, ignore_index=ignore_index,
                  verify_integrity=verify_integrity,
                  sort=sort)
ALollz
  • 48,033
  • 6
  • 42
  • 62
  • Thank you so much for your answer and link. But would you care to elaborate how this source code is about pd.append since there seemingly is no part denoting "append"? If possible, could you help me explain what `other` means and does in the code please? Thanks again. – Bowen Liu Sep 17 '18 at 15:50
  • @BowenLiu Sure. I just copied a small part of the `append` function. The full function is highlighted [here](https://github.com/pandas-dev/pandas/blob/v0.23.4/pandas/core/frame.py#L6075-L6211), with the last few lines above being the return statement for `pd.append`. If you look at the full source code you can see that if you call `df.append(df1)`, then `df` is treated as `self` and `other` will reference `df1`, the `DataFrame` or `Series` to append. So `to_concat` is basically just a list of the `DataFrames`, `[df, df1]`, which is passed to `pd.concat` – ALollz Sep 17 '18 at 15:56