简体   繁体   中英

Add a column to a pandas data frame using a pandas operation

If I have a data frame, and I need to perform some operation on a given column and produce a new column, is there a better way than the function below?

I do NOT want to alter the original column. I want to keep appending new columns for this and any similar operations.

But in the code below, it seems there are just too many lines. That is, the rank() function in pandas is super convenient. Seems to me there should be some parameter somewhere that says to the data frame, "Hey, apply this function you already know about, but instead of doing to the original column itself, as you do it, make it a new column at the end of the data frame"

Is there such a way? Or is there any way to make the code below more brief/elegant and achieve the same result? What I have just seems verbose. I do this for other things too, eg I have the same type of function for cut(). I will be doing it for a few other ops. Seems so common it should be easier.

Thanks!

def rank(pdfAll, nOldColIndex, sNewColName, sMethod, bAsc):
"""Appends a ranked column to a DataFrame based on an existing column.  

   nOldColIndex is the index of the column with the original data.
   sNewColName is the name of the new column.  
   sMethod goes to the pandas rank function to influence ranking behavior.
   bAsc goes to the pandas rank function to influence ranking behavior.
   pdfAll[nOldColIndex] must have numeric contents.

"""

serOldCol = pdfAll.ix[:,nOldColIndex]
serOldCol.name = sNewColName

serNewCol = serOldCol.rank(method=sMethod, ascending=bAsc)
pdfNewCol = pd.DataFrame(serNewCol)

pdfAll = pd.merge(pdfAll, pdfNewCol, left_index=True, right_index=True)

return pdfAll 

I'm not sure what this generalization is all about, but do you by any chance are trying to do something as

df['newColumn'] = df.oldColumn.rank()

Generalizing the function, if you want to do something on a row basis, you can do

df.apply(lambda x: x.oldColumn * x.otherOldColumn, axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM