Pandas create new column based on existing column
In SQL, we can just use SELECT statement to create a new column. In Python, we can do the same using the Pandas. For using Pandas, first create a custom function for how you need the new column to be created.
I had an scenario where I have to classify the person as Adult or Child. For this purpose I will be using the Age column in my data-set. If the age is above 18 and I will create the corresponding value as Adult in my new column. So let's see how this can be achieved.
def checkAdult(age):
if age>=18:
return Adult
else:
return Child
Above is my custom function. Where it takes one argument age and returns Adult or child.
The above Python function can be used in the existing data frame (data) to create new column(Adult/Child).
data[Adult/Child]=data[Age].apply(checkAdult)
I am creating new column with name Adult/Child. I am passing Age column to the checkAdult function as define in the right hand side.
I had an scenario where I have to classify the person as Adult or Child. For this purpose I will be using the Age column in my data-set. If the age is above 18 and I will create the corresponding value as Adult in my new column. So let's see how this can be achieved.
def checkAdult(age):
if age>=18:
return Adult
else:
return Child
Above is my custom function. Where it takes one argument age and returns Adult or child.
The above Python function can be used in the existing data frame (data) to create new column(Adult/Child).
data[Adult/Child]=data[Age].apply(checkAdult)
I am creating new column with name Adult/Child. I am passing Age column to the checkAdult function as define in the right hand side.
Comments
Post a Comment