calculate the difference between row “nl10107” and row “nl10318”. in python

You can use pandas dataframe and loc[] method to select the two rows and then calculate the difference.

Here's an example:

main.py
import pandas as pd

# create a dataframe
data = {'code': ['nl10107', 'nl10234', 'nl10318'],
        'value': [100, 200, 150]}
df = pd.DataFrame(data)

# select the two rows and calculate the difference
diff = df.loc[df['code'] == 'nl10107', 'value'].values[0] - df.loc[df['code'] == 'nl10318', 'value'].values[0]

print(diff)
327 chars
12 lines

Output:

main.py
-50
4 chars
2 lines

The output is -50, which is the difference between the value of row "nl10107" and row "nl10318".

related categories

gistlibby LogSnag