Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Simultaneous Replacement | Preprocessing Data: Part I
Data Manipulation using pandas

bookSimultaneous Replacement

The method described in the previous chapter allows you to replace specific values in one column 'manually'. But we need to perform replacements in 4 columns, which means we need to repeat the actions at least 3 more times.

However, pandas predicted that task, too. Let's consider the method that allows to perform replacement for all dataframe columns.

1
df.where(condition, other = values_to_replace, inplace = False)
copy

Explanation: condition is the first parameter, if True, then keeps original values, if False, then replaces them by values specified in the other parameter. inplace - if True, then rewrites the data. If you want to 'revert' the condition to opposite, place the ~ symbol in front of it. For instance, let's replace all the zeros with the word null.

12345678
# Importing the library import pandas as pd # Reading the file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f2947b09-5f0d-4ad9-992f-ec0b87cd4b3f/data1.csv') # Replace 0s by words 'null' df = df.where(~(df == 0), other = 'null') print(df)
copy

As you can see, there are many 'null's appeared in the dataframe. If you remove the ~ symbol within the .where() method, then all values but 0 will be replaced to 'null'.

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 7

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 2.56

bookSimultaneous Replacement

Pyyhkäise näyttääksesi valikon

The method described in the previous chapter allows you to replace specific values in one column 'manually'. But we need to perform replacements in 4 columns, which means we need to repeat the actions at least 3 more times.

However, pandas predicted that task, too. Let's consider the method that allows to perform replacement for all dataframe columns.

1
df.where(condition, other = values_to_replace, inplace = False)
copy

Explanation: condition is the first parameter, if True, then keeps original values, if False, then replaces them by values specified in the other parameter. inplace - if True, then rewrites the data. If you want to 'revert' the condition to opposite, place the ~ symbol in front of it. For instance, let's replace all the zeros with the word null.

12345678
# Importing the library import pandas as pd # Reading the file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f2947b09-5f0d-4ad9-992f-ec0b87cd4b3f/data1.csv') # Replace 0s by words 'null' df = df.where(~(df == 0), other = 'null') print(df)
copy

As you can see, there are many 'null's appeared in the dataframe. If you remove the ~ symbol within the .where() method, then all values but 0 will be replaced to 'null'.

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 7
some-alt