Pandas dataframe: slicing column values using second column for slice index

sam123

I'm trying to create a column of microsatellite motifs in a pandas dataframe. I have one column that gives the length of the motif and another that has the whole microsatellite.

Here's an example of the columns of interest.

     motif_len    sequence
0    3            ATTATTATTATT
1    4            ATCTATCTATCT
2    3            ATCATCATCATC

I would like to slice the values in sequence using the values in motif_len to give a single repeat(motif) of each microsatellite. I'd then like to add all these motifs as a third column in the data frame to give something like this.

     motif_len    sequence        motif
0    3            ATTATTATTATT    ATT
1    4            ATCTATCTATCT    ATCT
2    3            ATCATCATCATC    ATC

I've tried a few things with no luck.

>>df['motif'] = df.sequence.str[:df.motif_len]
>>df['motif'] = df.sequence.str[:df.motif_len.values]

Both make the motif column but all the values are NaN.

I think I understand why these don't work. I'm passing a series/array as the upper index in the slice rather than the a value from the mot_len column.

I also tried to create a series by iterating through each Any ideas?

EdChum

You can call apply on the df pass axis=1 to apply row-wise and use the column values to slice the str:

In [5]:
df['motif'] = df.apply(lambda x: x['sequence'][:x['motif_len']], axis=1)
df

Out[5]:
   motif_len      sequence motif
0          3  ATTATTATTATT   ATT
1          4  ATCTATCTATCT  ATCT
2          3  ATCATCATCATC   ATC

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Slicing pandas dataframe on equal column values

From Dev

Efficient strided slicing along a column in a pandas dataframe

From Dev

Excel to Pandas DataFrame using first column as index

From Dev

Filter a Pandas DataFrame on a text column using a concatenation of other column values

From Dev

Slice Pandas dataframe by index values that are (not) in a list

From Dev

Slice pandas dataframe based on datetime column

From Dev

Slice a Pandas dataframe by an array of indices and column names

From Dev

Slice pandas dataframe json column into columns

From Dev

Slice a Pandas dataframe by an array of indices and column names

From Dev

Slice pandas dataframe based on datetime column

From Java

Pandas Dataframe row selection combined condition index- and column values

From Dev

Pandas DataFrame - Combining one column's values with same index into list

From Dev

How to create a column depending on the row index values in a multiindex pandas dataframe?

From Dev

Creating pandas dataframe with datetime index and random values in column

From Dev

Pandas Dataframe row selection combined condition index- and column values

From Dev

How to create a column depending on the row index values in a multiindex pandas dataframe?

From Dev

How to add '$' to my pandas dataframe values and use a column as index?

From Dev

Pandas dataframe - Rearranging row index values into column headers

From Dev

Changing values in multiple columns of a pandas DataFrame using known column values

From Dev

Insert values in pandas dataframe using list values corresponding to column

From Dev

slice pandas dataframe by column showing all except the column provided

From Java

Replacing column values in a pandas DataFrame

From Dev

Pandas DataFrame column values in to list

From Dev

Demean column values of a pandas DataFrame

From Dev

How to get the cell at a location in pandas dataframe using a column as the index

From Dev

Reshaping pandas dataframe using pivot and provide multiple column as index

From Dev

Pandas DataFrame filtering based on second column

From Dev

Pandas DataFrame filtering based on second column

From Dev

New pandas dataframe column using values from python dictionary

Related Related

  1. 1

    Slicing pandas dataframe on equal column values

  2. 2

    Efficient strided slicing along a column in a pandas dataframe

  3. 3

    Excel to Pandas DataFrame using first column as index

  4. 4

    Filter a Pandas DataFrame on a text column using a concatenation of other column values

  5. 5

    Slice Pandas dataframe by index values that are (not) in a list

  6. 6

    Slice pandas dataframe based on datetime column

  7. 7

    Slice a Pandas dataframe by an array of indices and column names

  8. 8

    Slice pandas dataframe json column into columns

  9. 9

    Slice a Pandas dataframe by an array of indices and column names

  10. 10

    Slice pandas dataframe based on datetime column

  11. 11

    Pandas Dataframe row selection combined condition index- and column values

  12. 12

    Pandas DataFrame - Combining one column's values with same index into list

  13. 13

    How to create a column depending on the row index values in a multiindex pandas dataframe?

  14. 14

    Creating pandas dataframe with datetime index and random values in column

  15. 15

    Pandas Dataframe row selection combined condition index- and column values

  16. 16

    How to create a column depending on the row index values in a multiindex pandas dataframe?

  17. 17

    How to add '$' to my pandas dataframe values and use a column as index?

  18. 18

    Pandas dataframe - Rearranging row index values into column headers

  19. 19

    Changing values in multiple columns of a pandas DataFrame using known column values

  20. 20

    Insert values in pandas dataframe using list values corresponding to column

  21. 21

    slice pandas dataframe by column showing all except the column provided

  22. 22

    Replacing column values in a pandas DataFrame

  23. 23

    Pandas DataFrame column values in to list

  24. 24

    Demean column values of a pandas DataFrame

  25. 25

    How to get the cell at a location in pandas dataframe using a column as the index

  26. 26

    Reshaping pandas dataframe using pivot and provide multiple column as index

  27. 27

    Pandas DataFrame filtering based on second column

  28. 28

    Pandas DataFrame filtering based on second column

  29. 29

    New pandas dataframe column using values from python dictionary

HotTag

Archive