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

Equivalent of "table" of R in python

Equivalent of R/ifelse in Python/Pandas? Compare string columns?

python equivalent of R table

equivalent of a python dict in R

Python Pandas VLookup with multiple columns equivalent

Python equivalent for do.call(rbind, lapply()) from R

What would be Python/pandas equivalent of this R code for rearranging columns of a dataframe?

R sapply/lapply with multiple ifelse statements

Lapply while skipping specific columns R

lapply() to use a function over multiple columns of a dataframe

Efficient python pandas equivalent/implementation of R sweep with multiple arguments

lapply() output as a dataframe of multiple functions - R

Pandas equivalent of vlookup for multiple columns

r data.table lapply with multiple SDcols

Excel Formula: What is the equivalent of =MATCH(), but for multiple columns?

R store output from lapply with multiple functions

R manipulating multiple data frames using a lapply

Equivalent of CONCAT_GROUP for multiple columns

R: Lapply multiple equality

lapply equivalent function in python

Split column to multiple columns by using R or python

R qt equivalent in python

Python: R equivalent code for multiple values assignment of dataframe

Equivalent "break" for the lapply function

using lapply for a function on a matrix with columns as elements in R

R equivalent of matrix in Python

R lapply ifelse with multiple statements on list of dataframes

Split string column to multiple columns in R/Python

Is it possible to update multiple datasets using lapply in R?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive