R lapply on multiple columns to Python equivalent?

FrozenEve

I'm currently trying to find a pythonic way to "translate" the below R code which takes in a dataframe with three columns titled "strikeDeg", "dipDeg" and "rakeDeg" and appends it with three columns:

  • rotation = 9-element vector
  • pole = 3-element vector
  • direction = 3-element vector

This is part of a larger if-loop to parse the original dataframe depending on what columns are not NULL.

geoPoleDirectionRotationFromData <- function(dataFrame) {
    newFrame <- emptyDataFrame(nrow(dataFrame))
      # Make rotation, pole, direction from strike, dip, rake.
    newFrame$rotation <- lapply(1:nrow(dataFrame),
                              function(i) geoRotationFromStrikeDipRakeDeg(
                                c(dataFrame[i,]$strikeDeg, dataFrame[i,]$dipDeg, dataFrame[i,]$rakeDeg)))
    newFrame$pole <- lapply(1:nrow(newFrame), function(i) newFrame$rotation[[i]][1,])
    newFrame$direction <- lapply(1:nrow(newFrame), function(i) newFrame$rotation[[i]][2,])
    newFrame

The part of the code in question is how can I perform lapply() functions in each of the 3 new columns on the 3 existing columns of the input dataFrame in python? Would I have to create a vector from the existing columns of the dataFrame and apply a map() function?

David S

you can just use the apply function, and specify axis to match the dimension you want to apply it.

you can see an example in the following link

simple example for applying function on column:

import pandas as pd

matrix = [(222, 34, 23),
         (333, 31, 11),
         (444, 16, 21),
         (555, 32, 22),
         (666, 33, 27),
         (777, 35, 11)
         ]

# Create a DataFrame object
dfObj = pd.DataFrame(matrix, columns=list('abc'))
print(dfObj)

modDfObj = dfObj.apply(lambda x : x + 10) 
print(modDfObj)

I get the following output:

     a   b   c
0  222  34  23
1  333  31  11
2  444  16  21
3  555  32  22
4  666  33  27
5  777  35  11

     a   b   c
0  232  44  33
1  343  41  21
2  454  26  31
3  565  42  32
4  676  43  37
5  787  45  21

You can see that the only change was in the all the columns and you can easily generalize it to your desire. (for instance) for applying to a specific column or columns you can change the apply line to either options:

modDfObj = dfObj.apply(lambda x: x + 10 if x.name == 'b' else x)
modDfObj = dfObj.apply(lambda x: x + 10 if (x.name == 'b' or x.name == 'c') else x) 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related