创建一个列,该列为另一行中的更改增加值

时间:2017-11-26 03:16:40

标签: python pandas

我有一个包含两列的数据框,如下所示:

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
class ViewHolderFolders extends RecyclerView.ViewHolder {
    ...
    public ViewHolderFolders(View itemView){
    ...
    }
}

class ViewHolderFiles extends RecyclerView.ViewHolder {
    ...
    public ViewHolderFiles(View itemView){
    ...
}

@Override
public int getItemViewType(int position) {
    //Let us say you return 0 for folders and 1 for files
    //This is just an example you could write your own logic but make sure to  differenciate the two
    //Folders and Files in here are model class used to populate the 
    //recyclerview with. This is just an example.
    if (yourDataSet.get(position) instanceof Folders) {
        return 0;
    } else{
        return 1;
    }

}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     switch (viewType) {
         case 0: return new ViewHolderFolders(...);
         case 1: return new ViewHolderFiles(...);
         //Your code here
     }
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    switch (holder.getItemViewType()) {
        case 0:
            ViewHolderFolders viewHolderFolders = (ViewHolderFolders)holder;
            ...
            break;

        case 1:
            ViewHolderFiles viewHolderFiles = (ViewHolderFiles)holder;
            ...
            break;
    }
}

我想创建第三列,其值对于另一列的每次更改都会增加一。

Var1Var2
a   28
b   28
d   28
f   29
f   29
e   30
b   30
m   30
l   30
u   31
t   31
t   31

我将如何做到这一点?

2 个答案:

答案 0 :(得分:5)

使用category

df.Var2.astype('category').cat.codes.add(1)
Out[525]: 
0     1
1     1
2     1
3     2
4     2
5     3
6     3
7     3
8     3
9     4
10    4
11    4
dtype: int8

更新了

from itertools import groupby
grouped = [list(g) for k, g in groupby(df.Var2.tolist())]
np.repeat(range(len(grouped)),[len(x) for x in grouped])+1

答案 1 :(得分:3)

这样的事情:

class NumbersEntered {

public double total = 0; //declare as accessable field

public  void operatorSeletion(double number1, double number2, double number3, char operator)
{     
    switch(operator)
    {
        case '+':
            total = number1 + number2 + number3;
            break;

        case '-':
            total = number1 - number2 - number3;
            break;

        case '*':
            total = number1 * number2 * number3;
            break;

        case '/':
            total = number1 / number2 / number3;
            break;

        default:
            System.out.println("You have entered incorrectly. Please try again.");

            return;
    }       
}
}
public class JavaPresentation_KS {


public static void main(String[] args) {

    NumbersEntered nums = new NumbersEntered();
    Scanner equation = new Scanner(System.in);

    System.out.println("Enter first number: ");
    double number1 = equation.nextDouble();
    System.out.println("Enter second number: ");
    double number2 = equation.nextDouble();
    System.out.println("Enter third number: ");
    double number3 = equation.nextDouble();
    System.out.println("Enter an operator (+, -, *, /): ");
    char operator = equation.next().charAt(0);

    nums.operatorSeletion(number1, number2, number3, operator); 
//invoke the method

    System.out.println(number1 + " " + operator + " " + number2 + " " + operator + " " + number2 + " = " + nums.total);
//Access at location (nums.total)   
    }