How to replicate same values based on the index value of other column in python

user86907

I have a dataframe like below and I want to add another column that is replicated untill certain condition is met.

sample_df = pd.DataFrame(data={
  'id': ['A', 'B', 'C'],
  'n' : [  1,   2,   3],
  'v' : [ 10,  13,   8],
  'z' : [5,    3,    6],
  'g' : [8,    8,    10]
})

additional_rows=

Now I want to add another column which contains additional information about the dataframe. For instance, I want to replicate Yes untill id is B and No when it is below B and Yes from C to D and from from D to E Maybe.

The output I am expecting is as follows:

sample_df = pd.DataFrame(data={
  'id': ['A', 'B', 'C','G','D','E'],
  'n' : [  1,   2,   3, 5,  5,  9],
  'v' : [ 10,  13,   8, 8,  4 ,  3],
  'z' : [5,    3,    6, 9,  9,   8],
  'New Info': ['Yes','Yes','No','No','Maybe','Maybe']
})

sample_df

id  n   v   z   New Info
0   A   1   10  5   Yes
1   B   2   13  3   Yes
2   C   3   8   6   No
3   G   5   8   9   No
4   D   5   4   9   Maybe
5   E   9   3   8   Maybe

How can I achieve this in python?

David Erickson

You can use np.select to return results based on conditions. Since you were talking more about positional conditions I used df.index:

sample_df = pd.DataFrame(data={
  'id': ['A', 'B', 'C','G','D','E'],
  'n' : [  1,   2,   3, 5,  5,  9],
  'v' : [ 10,  13,   8, 8,  4 ,  3],
  'z' : [5,    3,    6, 9,  9,   8]
})

sample_df['New Info'] = np.select([sample_df.index<2, sample_df.index<4],['Yes', 'No'], 'Maybe')
sample_df
Out[1]: 
  id  n   v  z  New Info
0  A  1  10  5  Yes  
1  B  2  13  3  Yes  
2  C  3  8   6  No   
3  G  5  8   9  No   
4  D  5  4   9  Maybe
5  E  9  3   8  Maybe

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to replicate rows based on value of a column in same pandas dataframe

From Dev

How to update value in column cells based on other column cells in the same csv file bys using python?

From Dev

In Oracle SQL, How to replicate rows based on the binary value in a column?

From Dev

SQL- Change column value based on other column values in same group & different row

From Dev

Return the occurance of a value in a column based on other values

From Dev

How can I get a value from other dataframe's column based on other index?

From Dev

How to get value from python dataframe based on column index?

From Dev

Query based on index value or value in a column in python

From Dev

Query based on index value or value in a column in python

From Dev

Calculating sum(column) based on other column values in the same table

From Dev

Dropping duplicates based on other column values (Python)

From Dev

Python Pandas finding column value based on multiple column values in same data frame

From Dev

In Pandas How to sort one level of a multi-index based on the values of a column, while maintaining the grouping of the other level

From Dev

How to filter based on column values in other rows?

From Dev

Update column value that has one or more same values of other column

From Dev

How to write this SQL Server syntax to update a column based on the values of 2 other columns in that same table

From Dev

Update value in column based on other column values in Spark

From Dev

How to sort the values in one column based on other column values?

From Dev

Python Pandas create column based on value of index

From Java

Filter df based on multiple column values of other columns in the same df

From Dev

How to decreasing value based on other column in SQL

From Dev

How to find table names which have a same value in other tables based aone column

From Dev

How to null values based on an other field value

From Java

Python Filling dataframe values based on Column Index present in another dataframe value

From Dev

Drop pandas rows if value is not between two other values on the same column

From Dev

Select column value that matches a combination of other columns values on the same table

From Dev

Combining rows on a Dataframe based on a specific column value and add other values

From Dev

Time difference between two values based on other column value in SQL

From Dev

how can i assign no in column A based on Value in B and if there is any Duplicated values assign the same no.?

Related Related

  1. 1

    How to replicate rows based on value of a column in same pandas dataframe

  2. 2

    How to update value in column cells based on other column cells in the same csv file bys using python?

  3. 3

    In Oracle SQL, How to replicate rows based on the binary value in a column?

  4. 4

    SQL- Change column value based on other column values in same group & different row

  5. 5

    Return the occurance of a value in a column based on other values

  6. 6

    How can I get a value from other dataframe's column based on other index?

  7. 7

    How to get value from python dataframe based on column index?

  8. 8

    Query based on index value or value in a column in python

  9. 9

    Query based on index value or value in a column in python

  10. 10

    Calculating sum(column) based on other column values in the same table

  11. 11

    Dropping duplicates based on other column values (Python)

  12. 12

    Python Pandas finding column value based on multiple column values in same data frame

  13. 13

    In Pandas How to sort one level of a multi-index based on the values of a column, while maintaining the grouping of the other level

  14. 14

    How to filter based on column values in other rows?

  15. 15

    Update column value that has one or more same values of other column

  16. 16

    How to write this SQL Server syntax to update a column based on the values of 2 other columns in that same table

  17. 17

    Update value in column based on other column values in Spark

  18. 18

    How to sort the values in one column based on other column values?

  19. 19

    Python Pandas create column based on value of index

  20. 20

    Filter df based on multiple column values of other columns in the same df

  21. 21

    How to decreasing value based on other column in SQL

  22. 22

    How to find table names which have a same value in other tables based aone column

  23. 23

    How to null values based on an other field value

  24. 24

    Python Filling dataframe values based on Column Index present in another dataframe value

  25. 25

    Drop pandas rows if value is not between two other values on the same column

  26. 26

    Select column value that matches a combination of other columns values on the same table

  27. 27

    Combining rows on a Dataframe based on a specific column value and add other values

  28. 28

    Time difference between two values based on other column value in SQL

  29. 29

    how can i assign no in column A based on Value in B and if there is any Duplicated values assign the same no.?

HotTag

Archive