Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I would like to change every second row of my data frame.

I have a df like this:

 Node  |  Feature | Indicator | Value | Class | Direction
--------------------------------------------------------
1     |  WPS     |     <=    | 0.27  | 4     | 1 -> 2  
--------------------------------------------------------
2     |  ABC     |     <=    | 0.40  | 5     | 1 -> 3
--------------------------------------------------------
3     |  CXC     |     <=    | 0.45  | 2     | 2 -> 4
--------------------------------------------------------
4     |  DFT     |     <=    | 0.56  | 1     | 2 -> 5
--------------------------------------------------------
5     |  KPL     |     <=    | 0.30  | 3     | 3 -> 5
--------------------------------------------------------
6     |  ERT     |     <=    | 0.55  | 5     | 3 -> 1

I would like the following:

 Node  |  Feature | Indicator | Value | Class | Direction
--------------------------------------------------------
1     |  WPS     |     <=    | 0.27  | 4     | 1 -> 2  
--------------------------------------------------------
2     |  WPS     |     >     | 0.27  | 5     | 1 -> 3
--------------------------------------------------------
3     |  CXC     |     <=    | 0.45  | 2     | 2 -> 4
--------------------------------------------------------
4     |  CXC     |     >     | 0.45  | 1     | 2 -> 5
--------------------------------------------------------
5     |  KPL     |     <=    | 0.30  | 3     | 3 -> 5
--------------------------------------------------------
6     |  KPL     |     >     | 0.30  | 5     | 3 -> 1

So every second row changes the 'Feature' and 'Value' into the same as the row above, and the 'Indicator' is changed to '>'

I can't figure out how to iterate through the Dataframe (using iterrows I suppose) and changing only every second row?

EDIT:

I have tried the following as recommended:

    my_df = pd.DataFrame()
    my_df['N'] = [1, 2, 3, 4, 5, 6]
    my_df['I'] = ['=>', '=>', '=>', '=>', '=>', '=>']
    my_df['F'] = ['a', 'b', 'c', 'd', 'e', 'f']

    my_df.loc[1::2, 'F'] = None
    my_df.loc[1::2, 'I'] = '>'

    my_df.fillna(method='ffill')

    print(my_df)

Output:

   N   I     F
0  1  =>     a
1  2   >  None
2  3  =>     c
3  4   >  None
4  5  =>     e
5  6   >  None
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
151 views
Welcome To Ask or Share your Answers For Others

1 Answer

Find below is the logic used

  • Select the even rows using slicing.
  • Set the desired columns / fields with None for the sliced rows which need to be borrowed from previous row, we can fill it later using forward fill.
  • Then use forward fill from last non null fields for respective columns
import pandas as pd
xlsColName = chr(ord('A')+colPosn)       # Get xls column name (not the column header as per data frame). This will be used to set attributes of xls columns
df = pd.read_csv('temp.csv')
df.loc[1::2, 'Feature'] = None           # prepare the field for use with df.fillna
df.loc[1::2, 'Value'] = None
df.loc[1::2, 'Indicator'] = '>'          # update the indicator field
df.fillna(method='ffill', inplace=True)  # This fills the NaN values from existing values 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...