.idxmax相当于一个特定的标志?

时间:2017-11-11 22:44:04

标签: python pandas dataframe

我正在尝试返回在最后一次出现特定数字(Flag)之后发生的列标题,该标题可能存在于行中,也可能不存在

例如

// C Libraries Used

#include <stdio.h>
#include <math.h>
#include <string.h>

// Constant declerations

const float OTPAYFACTOR = 1.5;
FILE *userinputfile;                  //disk file (for input)

// Variable declerations
char deptname [21];
char firstname [10];
char lastname [10];
char fullname [21];
float hrsworked;
float hrwage;
int count;
char again;

// Function Prototypes


// M A I N   F U N C T I O N

int main (void){
    printf("Mountain Pacific Corporation\nDepartment Salary Program\n\n");
    printf("Please enter the name of the department: ");
    scanf("%s", &deptname);
    count = 0;    // Initialize this "counting" variable to zero to start
    printf("\n");
    count = 0;    // Initialize this "counting" variable to zero to start
    printf("\n");
   do {

      count++; // Increment the counting variable

      printf("Enter employee #%d: ", count);
      scanf("%s %s", &firstname, &lastname);
      strcpy(fullname, firstname);
      strcat(fullname, " ");
      strcat(fullname, lastname);
      printf("Enter the hourly wage of %s: ", fullname);
      scanf("%f", &hrwage);
      printf("Enter total number of hours: ");
      scanf("%f", &hrsworked);

      printf("\nThank you. Process another employee? ");
      scanf ("%s", &again);
      printf("\n");

} while (again != 'N' && again != 'n');

      printf("End of processing.");

    printf("%s, $%0.2f, %0.2f: \n", fullname, hrwage, hrsworked);

return 0;
}

我想填写'Flag'栏中的内容:

A     B     C     D   Flag
1     2     3     4       
Flag  1     2     3       
5     70    3     1        
Flag Flag   2     1       

非常感谢!

1 个答案:

答案 0 :(得分:1)

replace标记为np.nan并使用first_valid_index(PS:如果您想填写空白,可以使用fillna(''),我想保留它。)< / p>

df1=df.replace({'Flag':np.nan})
df['Flag']=df1.apply(lambda x: x.first_valid_index(),axis=1)[df1.isnull().any(1)]
df
Out[211]: 
      A     B  C  D Flag
0     1     2  3  4  NaN
1  Flag     1  2  3    B
2     5    70  3  1  NaN
3  Flag  Flag  2  1    C