divide alternating rows in table in python

You can divide alternating rows in a table by using pandas library in Python. Here's an example code:

main.py
import pandas as pd

# create sample dataset
data = {'employee_id': [101, 102, 103, 104, 105, 106],
        'employee_name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva', 'Frank'],
        'department': ['Sales', 'Marketing', 'Sales', 'Human Resource', 'Marketing', 'Human Resource']}
df = pd.DataFrame(data)

# divide alternating rows using loc attribute and boolean indexing
df.loc[::2, 'employee_id':'department'] = df.loc[::2, 'employee_id':'department'].apply(lambda x: x.str.upper())

# display updated dataset
print(df)
525 chars
14 lines

In this code, we use the loc attribute to select every other row, starting from the first row (i.e., ::2). We then specify the columns we want to update (i.e., employee_id to department) using slicing. Finally, we use the apply() method with a lambda function to uppercase the values in the selected rows.

Note that this code assumes that the table has a header row, and that the first row is at index 0. Also, you can modify the column names and the condition for selecting rows depending on your specific use case.

gistlibby LogSnag