How to Create a Dataframe Column With Array in Python

To create a dataframe column with an array in Python, you can use the assignment operator or the apply() function.

The following example shows how to create a dataframe column with an array using two different methods in Python.

Using Assignment Operator

We can use the assignment operator to create a dataframe column with an array.

Suppose we have the following dataframe:

# Import pandas library
import pandas as pd

# Create dataframe
Office_Stuff = pd.DataFrame({
    'Date': ['01-03-2023', '01-03-2023', '01-03-2023', '01-03-2023', '02-03-2023', '02-03-2023'],
    'Product_Code': ['A-101', 'A-102', 'A-103', 'B-101', 'B-102', 'B-104'],
    'Product_Name': ['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse'],
    'Price': [4500, 550, 250, 50, 350, 50],
    'Status': [1, 1, 1, 0, 1, 1]
})

# Create array
status_array = ['Working', 'Working', 'Working', 'Not Working', 'Working', 'Working']

# Add new column to dataframe
Office_Stuff['Status_Text'] = status_array

# Show updated dataframe
print(Office_Stuff)

Output: 👇️

         Date Product_Code Product_Name  Price  Status  Status_Text
0  01-03-2023        A-101       Laptop   4500       1      Working
1  01-03-2023        A-102       Mobile    550       1      Working
2  01-03-2023        A-103      Printer    250       1      Working
3  01-03-2023        B-101     Keyboard     50       0  Not Working
4  02-03-2023        B-102      Scanner    350       1      Working
5  02-03-2023        B-104        Mouse     50       1      Working

In this example, we use the assignment operator to add a new column Status_Text to the dataframe Office_Stuff using the array status_array.

Using apply() Function

We can use the apply() function to create a dataframe column with an array.

Suppose we have the following dataframe:

# Import pandas library
import pandas as pd

# Create dataframe
Office_Stuff = pd.DataFrame({
    'Date': ['01-03-2023', '01-03-2023', '01-03-2023', '01-03-2023', '02-03-2023', '02-03-2023'],
    'Product_Code': ['A-101', 'A-102', 'A-103', 'B-101', 'B-102', 'B-104'],
    'Product_Name': ['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse'],
    'Price': [4500, 550, 250, 50, 350, 50],
    'Status': [1, 1, 1, 0, 1, 1]
})

# Create array
status_array = ['Working', 'Working', 'Working', 'Not Working', 'Working', 'Working']

# Add new column to dataframe
Office_Stuff['Status_Text'] = Office_Stuff.apply(lambda row: status_array[row.name], axis=1)

# Show updated dataframe
print(Office_Stuff)

Output: 👇️

         Date Product_Code Product_Name  Price  Status  Status_Text
0  01-03-2023        A-101       Laptop   4500       1      Working
1  01-03-2023        A-102       Mobile    550       1      Working
2  01-03-2023        A-103      Printer    250       1      Working
3  01-03-2023        B-101     Keyboard     50       0  Not Working
4  02-03-2023        B-102      Scanner    350       1      Working
5  02-03-2023        B-104        Mouse     50       1      Working

In this example, we use the apply() function to add a new column Status_Text to the dataframe Office_Stuff using the array status_array.

Conclusion

We can use the assignment operator or the apply() function to create a dataframe column with an array in Python.