ChangeCells
Change the values of one or more cells in a variety of different ways, including:
- cells at specied row / column locations
- entire columns
- entire rows
- cells that meet a specified criteria
- cells that match a specified value
- cells at specied row / column locations
- entire columns
- entire rows
- cells that meet a specified criteria
- cells that match a specified value
Options
columns: ColumnName(s) to be used, either a single column or a list of columns.
where: Condition to be met for setting the value.
value: Value to be set, can be a constant or a variable.
rows: Row indices to be used, either a single row or a list of rows.
initialValues: Use with --newValues, specifies the value to find in cells to replace
newValues: Use with --initialValues, specifies the replacement value to replace the --initialValues with
allMatchingRows: Flag indicating all rows matching the condition should be updated.
allColumns: Flag indicating all columns should be updated.
Examples
Example 1 - Change Cell by Row and Column
Change specific rows within one or more columns to a new value. In this case, in the column AcountID we are changing rows with index 2, 4, and 5 to Inactive to mark that those accounts are no longer used.
#> ChangeCells --rows 2 4 5 --columns AccountID --value Inactive
AFLEFT
bankTransactionsDf.loc[[2, 4, 5], 'AccountID' ] = 'Inactive' AFRIGHT
Example 2 - Change Cells by String Match and Replace
Modify all cells in a column that match a given string with a new value. In this case, we want to replace San Diego with Los Angeles in the column Location. If the entire cell was San Diego, than the entire cell would be change to Los Angeles. However, if the cell contained other text in addition to San Diego, the other text would remain the same, and only San Diego in the cell would change to Los Angeles.
#> ChangeCells --initialValues San Diego --newValues Los Angeles --columns Location
AFLEFT
bankTransactionsDf['Location'] = bankTransactionsDf['Location'].str.replace('San Diego', 'Los Angeles') AFRIGHT
Example 3 - Change Cells That Meet Condition
Set all cells in a column to a new value if the cell meets the desired condition. Here, the condition is that the TransactionDate in a row is after (greater than) 06-01-2023. Therefore, if the TransactionDate is after 06-01-2023, then we set the TransactionType cell to Cash in the row. This would replace then entire contents of the cell TransactionType in the row.
#> ChangeCells --columns TransactionType --value Cash --where TransactionDate > 06-01-2023 --allMatchingRows
AFLEFT
bankTransactionsDf['TransactionDate'] = pd.to_datetime(bankTransactionsDf['TransactionDate'])
bankTransactionsDf.loc[ bankTransactionsDf['TransactionDate'] > "06 - 01 - 2023", 'TransactionType' ] = 'Cash' AFRIGHT