0
$\begingroup$

I have such a dataframe, I want to group them both when they are consecutive and when they are the same. In fact, I would like to see every intervals in different names so that I can print them and see where the beds spread from start to finish.

Input:

 THICKNESS          DEPTH   
738 0.2                 50
739 0.2                 50.05
740 0.2                 50.10
741 0.2                 50.15
742 0.2                 50.20
800 0.10                120
801 0.10                120.05
802 0.10                120.1
950 0.15                170
951 0.15                170.05
952 0.15                170.1
953 0.15                170.15

Output Except:

BED1= (50,50.20)
BED2= (120,120.1)
BED3= (170,170.15)

Normally I can define If, Elif function to define names for every single interval. But the problem is that I want to use this codes for different databases. What I mean is that I cannot define such a function that using for every same variable. So, I should use lambda function. But Im struggling writing lambda function. Tried this kind of function but I got NaN result in dataframe.

df['BEDS'] = (
    df2.groupby(df2['THICKNESS'].ne(df2['THICKNESS'].shift())) 
      .transform(lambda x: x.diff().sum()))

Edit: The keypoint should be the thickness. It should say the depth value at the end and beginning of the constancy.

Edit2: These are my codes.

import pandas as pd

df = pd.read_excel('V131BLOG.xlsx')


df['THICKNESS'] = (
    df.groupby(df['LITHOLOGY'].ne(df['LITHOLOGY'].shift()).cumsum())['DEPTH_MD'] 
      .transform(lambda x: x.diff().sum()))


out = df[(df['LITHOLOGY'] == 1) & (df['THICKNESS'] >= 0.35)]

Output for these lines:

    Unnamed: 0  DEPTH_MD    CALIPER GR  LITHOLOGY   SHALLOW DEEP    WELL    THICKNESS
738 738 266.90  32.5626 11.34590    1   2.72669 2.49057 V131B   0.8
739 739 266.95  33.2834 3.81954 1   2.86810 2.60241 V131B   0.8
740 740 267.00  33.4193 11.42080    1   2.99825 2.70382 V131B   0.8
741 741 267.05  34.1252 3.84500 1   3.12187 2.78883 V131B   0.8
742 742 267.10  33.9583 3.84500 1   3.23839 2.85871 V131B   0.8
... ... ... ... ... ... ... ... ... ...
1618    1618    310.90  30.5434 22.69180    1   3.79042 3.25649 V131B   1.0
1619    1619    310.95  29.5919 22.69180    1   3.80181 3.27215 V131B   1.0
1620    1620    311.00  29.2208 11.30880    1   3.80493 3.28251 V131B   1.0
1621    1621    311.05  27.2846 26.64850    1   3.79330 3.28023 V131B   1.0
1622    1622    311.10  28.1289 26.47380    1   3.76142 3.26074 V131B   1.0

As you can see my dataframe has many rows and thickness values for LITHOLOGY column equals 1. I want to give consecutive nouns to every different thickness values.

$\endgroup$

1 Answer 1

0
$\begingroup$

The following should give the information you are looking for, the only difference is that the three fields (i.e. bed number, minimum value and maximum value of the 'DEPTH' field) are in seperate columns instead of concatenated into a single string, which should be easy if this is really what you need.

import pandas as pd

df = pd.DataFrame({
    "THICKNESS": [0.2, 0.2, 0.2, 0.2, 0.2, 0.1, 0.1, 0.1, 0.15, 0.15, 0.15, 0.15],
    "DEPTH": [50, 50.05, 50.1, 50.15, 50.2, 120, 120.05, 120.1, 170, 170.05, 170.1, 170.15]
})

(
    df
    # get the bed number by comparing rows in the 'THICKNESS' column
    .assign(BED = lambda x: (x["THICKNESS"] != x["THICKNESS"].shift()).cumsum())
    # get the minimum and maximum value of the 'DEPTH' column for each bed
    .groupby("BED")
    .agg(
        DEPTH_min = ("DEPTH", "min"),
        DEPTH_max = ("DEPTH", "max")
    )
    .reset_index()
)
BED DEPTH_min DEPTH_max
1 50 50.2
2 120 120.1
3 170 170.15
$\endgroup$
1
  • $\begingroup$ I combined your lines with my lines and It worked! Thanks! $\endgroup$ Jan 31, 2022 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.