1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Pandas: Update the rows of a dataframe using values in an array

Discussion in 'Programming/Internet' started by ElGranGeek, Sep 12, 2018.

  1. ElGranGeek

    ElGranGeek Guest

    I'm getting my feet wet in Pandas and I'm trying to achieve the following as coded in Python:

    import numpy as np
    import pandas as pd

    options = df.read_csv('Some file.csv')

    COLS = 4
    ROWS = 3

    pN = np.zeros(COLS)
    pP = np.zeros(COLS)

    for i in range(COLS):
    pref = np.nrandom.randint(1,5)
    for j in range(ROWS):
    if options[j] == pref:
    pN = j
    pP = pref


    Example:

    Array options is a sparse matrix:

    0 0 2 0
    0 1 0 4
    0 0 0 4


    For every column I generate a random number pref between 1 and 4. Then looping through the rows, if random number pref is equal to the value in a particular cell then arrays pN and pP get updated as

    pN = j
    pP = pref


    Say that for col=3, pref=4, then, for row=1, options[1][3] is equal to 4,

    pN[3] = 1
    pP[3] = 4


    and for row=2 because options[2][3] is also equal to 4 and pref is still 4,

    pN[3] = 2
    pP[3] = 4


    Given that I already have a data frame df created and the appropriate np.array(s) created, what is the best way to perform this operation in Pandas?

    Login To add answer/comment
     

Share This Page