简体   繁体   中英

How can I add a new line in pandas dataframe based in a condition?

I have this Dataframe that is populated from a file. The first column is always the same value, the second is dimension based (I got these values from a Cam file), and the third column is created by a else-if condition.

[1]   [2] [3]
  1     30  2
  1     30  1
  1     30  3
  1     90  3
  1    370  3
  1    430  3
  1    705  3
  1    805  3
  1    880  2
  1    905  3
  1   1005  3
  1   1170  3
  1   1230  3
  1   1970  3
  1   2030  3
  1   2970  3
  1   3030  3
  1   3970  3
  1   4030  3
  1   4423  3
  1   4539  3
  1   4575  3
  1   4630  2
  1   4635  3
  1   4671  3
  1   4787  3
  1   4957  3
  1   5057  3
  1   5270  3
  1   5330  3
  1   5970  3
  1   6030  3
  1   6970  3
  1   7030  3
  1   7970  3
  1   8030  3
  1   8158  3
  1   8257  3
  1   8332  2
  1   8357  3
  1   8457  3
  1   8970  3
  1   9030  3
  1   9970  3
  1  10030  3
  1  10970  3
  1  11030  3
  1  11470  3
  1  11530  3
  1  11853  3
  1  11953  3

Now I need to create a new row based in a calculation. I need to iterate each line to find a value that is greater than 100 to add a new line like this.. Taking for example the lines number 4 and 5:

  1     90  3
  1    370  3
 370 - 90 = 260 (260 is greater than 100)

So I need to add a new line with the last number + 100, and the last column needs to be zero:

  1     90  3
  1    190  0
  1    370  3

Any ideas how can I achieve that? Thanks in advance.

Edit: I just need to add the line once in the DataFrame.

Try:

m = df["[2]"].diff() > 100

df.loc[m, "[2]"] = pd.Series(
    [
        [str(df.iloc[v - 1]["[2]"] + 100), df.iloc[v]["[2]"]]
        for v in df.index[m]
    ],
    index=df.index[m],
)

df = df.explode("[2]")
df["[3]"] = np.where(
    df["[2]"].apply(lambda x: isinstance(x, str)), 0, df["[3]"]
)
df["[2]"] = df["[2]"].astype(int)
print(df)

Prints:

    [1]    [2]  [3]
0     1     30    2
1     1     30    1
2     1     30    3
3     1     90    3
4     1    190    0
4     1    370    3
5     1    430    3
6     1    530    0
6     1    705    3
7     1    805    3
8     1    880    2
9     1    905    3
10    1   1005    3
11    1   1105    0
11    1   1170    3
12    1   1230    3
13    1   1330    0
13    1   1970    3
14    1   2030    3
15    1   2130    0
15    1   2970    3
16    1   3030    3
17    1   3130    0
17    1   3970    3
18    1   4030    3
19    1   4130    0
19    1   4423    3
20    1   4523    0
20    1   4539    3
21    1   4575    3
22    1   4630    2
23    1   4635    3
24    1   4671    3
25    1   4771    0
25    1   4787    3
26    1   4887    0
26    1   4957    3
27    1   5057    3
28    1   5157    0
28    1   5270    3
29    1   5330    3
30    1   5430    0
30    1   5970    3
31    1   6030    3
32    1   6130    0
32    1   6970    3
33    1   7030    3
34    1   7130    0
34    1   7970    3
35    1   8030    3
36    1   8130    0
36    1   8158    3
37    1   8257    3
38    1   8332    2
39    1   8357    3
40    1   8457    3
41    1   8557    0
41    1   8970    3
42    1   9030    3
43    1   9130    0
43    1   9970    3
44    1  10030    3
45    1  10130    0
45    1  10970    3
46    1  11030    3
47    1  11130    0
47    1  11470    3
48    1  11530    3
49    1  11630    0
49    1  11853    3
50    1  11953    3

EDIT: To change only one value:

mask = df["[2]"].diff() > 100
if True in mask:
    m = [False] * len(df)
    m[mask.idxmax()] = True

    df.loc[m, "[2]"] = pd.Series(
        [
            [str(df.iloc[v - 1]["[2]"] + 100), df.iloc[v]["[2]"]]
            for v in df.index[m]
        ],
        index=df.index[m],
    )

    df = df.explode("[2]")
    df["[3]"] = np.where(
        df["[2]"].apply(lambda x: isinstance(x, str)), 0, df["[3]"]
    )
    df["[2]"] = df["[2]"].astype(int)
    print(df)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM