Let’s say we have the following DataFrame:
A 0 1 1 2 2 3 3 4 4 5
The DataFrame has a single column named “A”. Let’s say we want to add two columns “B” and “C” in the DataFrame, such that the DataFrame becomes like this:
A B C 0 1 6 11 1 2 7 12 2 3 8 13 3 4 9 14 4 5 10 15
In this article, we will discuss how to add multiple columns in a DataFrame using the pandas Python library.
We can use the following Python code to add multiple columns in a DataFrame:
import pandas
import numpy
df = pandas.DataFrame([1, 2, 3, 4, 5], columns=["A"])
print("DataFrame before adding columns: \n", df)
# df[numpy.array(["B", "C"])] = pandas.DataFrame([[6, 11], [7, 12], [8, 13], [9, 14], [10, 15]])
df[numpy.array(["B", "C"])] = numpy.array([[6, 11], [7, 12], [8, 13], [9, 14], [10, 15]])
print("DataFrame after adding columns: \n", df)
Firstly, we are creating the DataFrame df that has one column A. After that, we are using the following Python statement …








































0 Comments