Home »
Python »
Python Programs
Add value at specific iloc into new dataframe column in pandas
Learn, how to add value at a specific iloc into new dataframe column in Python Pandas?
By Pranit Sharma Last updated : October 06, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Problem statement
Suppose that we are given a dataframe with some rows and columns and we perform some operations with each value which results in some new value, we need to store this new value in the new column of the dataframe at some correct position.
Adding value at specific iloc into new dataframe column
For this purpose, we can use the loc property with the row index and column name where we want to add the new value. This property is used to access a group of rows and columns by label(s) or a boolean array.
The syntax of loc property is:
property DataFrame.loc
Let us understand with the help of an example,
Python program to add value at specific iloc into new dataframe column in pandas
# Importing pandas package
import pandas as pd
# Creating a dataframe
df = pd.DataFrame(data={'X': [1, 6,5 ], 'Y': [1,8,7], 'Z': [5,0, 2.333]})
# Display the DataFrame
print("Original DataFrame:\n",df,"\n\n")
# Putting some new value at some position
df.loc[[0,2],'Z'] = 3
# Display modified DataFrame
print("Modified DataFrame:\n",df)
Output
The output of the above program is:
Python Pandas Programs »