[파이썬] pandas 데이터 프레임 병합 (append)

Pandas is a powerful library in Python used for data manipulation and analysis. One of its key functionalities is the ability to merge or append multiple data frames together. In this blog post, we will explore how to append data frames using pandas.

What is DataFrame append?

Appending data frames simply means combining two or more data frames vertically. It is similar to concatenating arrays in programming languages. The resulting data frame will have rows from all the input data frames.

Appending Data Frames using pandas

To append data frames in pandas, we can use the append() method. Here’s an example to illustrate how it works:

import pandas as pd

# Create two data frames
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': ['a', 'b', 'c']})
df2 = pd.DataFrame({'A': [4, 5, 6], 'B': ['d', 'e', 'f']})

# Append df2 to df1
df_appended = df1.append(df2, ignore_index=True)

print(df_appended)

Output:

   A  B
0  1  a
1  2  b
2  3  c
3  4  d
4  5  e
5  6  f

In the above example, we first create two data frames df1 and df2. Then, we use the append() method to append df2 to df1. The ignore_index=True parameter ensures the index is reset in the appended data frame.

Important Points to Note

Here are some important points to keep in mind when appending data frames:

  1. The data frames you want to append must have the same columns. If the columns are not the same, you might need to modify the data frames or handle missing/null values appropriately.

  2. Appending data frames can be an expensive operation, especially if the data frames are large. In such cases, it is recommended to use other techniques like concatenating or merging, which might be more efficient.

  3. The append() method does not modify the original data frames. It creates a new data frame with the appended rows.

  4. While appending data frames, it’s crucial to consider the indexing. The ignore_index=True parameter helps in resetting the index in the appended data frame.

Conclusion

Appending data frames is a handy technique in pandas when you need to combine multiple data frames vertically. By using the append() method, you can easily accomplish this task. Just ensure that the data frames you want to append have the same columns and handle the indexing appropriately.

Remember, pandas provides various other methods like merging and concatenating to combine data frames. Choose the appropriate method based on your specific requirements.

Happy coding with pandas! 🐼