Adjust every other row of a data frame

2019-09-20 00:56发布

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

标签: python pandas
2条回答
老娘就宠你
2楼-- · 2019-09-20 01:26

You an try something like:

df['Indicator']=">" if df['Node'] % 2 ==0 else "<="

This is if the 'Node' column is similar to and index.

查看更多
叛逆
3楼-- · 2019-09-20 01:40

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 
查看更多
登录 后发表回答