Pandas Information Body Insert technique
The information body insert technique is used so as to add a brand new column on the specified place in an information body.
If a newly added column identify already exists then an error happens.
As a way to add a reproduction column, it’s possible you’ll use the allow_duplicate= True
Syntax of insert technique
DataFrame.insert(loc, column, worth, allow_duplicates=_NoDefault.no_default)
An instance of the insert so as to add a brand new column
For our examples, we’ve the next information body created by utilizing this Python program:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
import pandas as pd
inflation_data = {
“12 months”: [2015, 2016, 2016, 2017],
“Inflation”: [‘1.75%’, ‘2%’, ‘3%’, ‘3.5%’]
}
#Making a Information body with two columns
df_inf = pd.DataFrame(inflation_data)
#Show Information Body
print(df_inf) |
Information Body:
|
12 months Inflation
0 2015 1.75%
1 2016 2%
2 2016 3%
3 2017 3.5% |
Within the instance under, we’ll solely use three arguments within the insert technique.
The primary argument specifies the place – the place so as to add the column.
It’s a 0-based index, so we offered 2 so as to add the column after the ID column.
Then we offered the column identify and the final argument incorporates the values for the brand new column.
Python program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
import pandas as pd
inflation_data = {
“12 months”: [2015, 2016, 2016, 2017],
“Inflation”: [‘1.75%’, ‘2%’, ‘3%’, ‘3.5%’]
}
#Making a Information body with two columns
df_inf = pd.DataFrame(inflation_data)
#Including a brand new column at second quantity within the DF
df_inf.insert(1, “Distinction”, [0.75, 0.25,1,0.5])
#Show Information Body with new column
print(df_inf) |
Output:
You possibly can see, the Distinction column is added after the primary column “12 months”.
The instance of including a reproduction column
As talked about earlier, Python will generate an error in case you strive including a column identify that already exists. See under the place we used the insert technique for including a column “Identify” within the above-created information body.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
import pandas as pd
inflation_data = {
“12 months”: [2015, 2016, 2016, 2017],
“Inflation”: [‘1.75%’, ‘2%’, ‘3%’, ‘3.5%’]
}
#Making a Information body with two columns
df_inf = pd.DataFrame(inflation_data)
#Including a brand new column at second quantity that exist already
df_inf.insert(1, “12 months”, [2018,2019,2020,2021])
#Show Information Body with new column
print(df_inf) |
Output:
|
increase ValueError(f“can not insert {column}, already exists”)
ValueError: can not insert 12 months, already exists |
So, it generated the ValueError.
Utilizing allow_duplicates within the insert technique
As a way to permit including a reproduction column, it’s possible you’ll use the allow_duplicates argument as follows:
allow_duplicates = True
See the identical instance as above and the output.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
import pandas as pd
inflation_data = {
“12 months”: [2015, 2016, 2016, 2017],
“Inflation”: [‘1.75%’, ‘2%’, ‘3%’, ‘3.5%’]
}
#Making a Information body with two columns
df_inf = pd.DataFrame(inflation_data)
#Including a brand new column at second quantity that exist already with allow_duplicates=True
df_inf.insert(1, “12 months”, [2018,2019,2020, 2021], allow_duplicates=True)
#Show Information Body with new column
print(df_inf) |
Output: