Home > Net >  Replace values from second row onwards in a pandas pipe method
Replace values from second row onwards in a pandas pipe method

Time:12-03

I am wondering how to replace values from second row onwards in a pipe method (connecting to the rest of steps).

import pandas as pd
import numpy as np

df = pd.DataFrame(
    {
        "Date": ["2020-01-01", "2021-01-01", "2022-01-01"],
        "Pop": [90, 70, 60],
    }
)

         Date  Pop
0  2020-01-01   90
1  2021-01-01   70
2  2022-01-01   60

Current solution

df.iloc[1:] = np.nan

Expected output

         Date  Pop
0  2020-01-01   90
1  2021-01-01  NaN
2  2022-01-01  NaN

CodePudding user response:

You can also use assign like this:

df.assign(Pop=df.loc[[0], 'Pop'])

Output:

         Date   Pop
0  2020-01-01  90.0
1  2021-01-01   NaN
2  2022-01-01   NaN

Note: assign works with nice column headers, if your headers have spaces or special characters you will need to use a different method.

CodePudding user response:

To replace the values from the second row onwards in your DataFrame using the pandas method pipe, you can do the following:

import pandas as pd
import numpy as np

df = pd.DataFrame(
    {
        "Date": ["2020-01-01", "2021-01-01", "2022-01-01"],
        "Pop": [90, 70, 60],
    }
)

# Use the `pipe` method to apply a function to your DataFrame
df = df.pipe(lambda x: x.iloc[1:]).replace(np.nan)

print(df)

This will replace the values from the second row onwards in your DataFrame with NaN. The resulting DataFrame will look like this:

         Date  Pop
0  2021-01-01  NaN
1  2022-01-01  NaN

Note that this will modify your original DataFrame in place, so if you want to keep the original DataFrame, you should make a copy of it before applying the pipe method. You can do this by using the copy method, like this:

import pandas as pd
import numpy as np

df = pd.DataFrame(
    {
        "Date": ["2020-01-01", "2021-01-01", "2022-01-01"],
        "Pop": [90, 70, 60],
    }
)

# Make a copy of the original DataFrame
df_copy = df.copy()

# Use the `pipe` method to apply a function to your DataFrame
df_copy = df_copy.pipe(lambda x: x.iloc[1:]).replace(np.nan)

print(df_copy)

This will create a copy of your DataFrame and then modify the copy, so the original DataFrame will remain unchanged. The resulting DataFrame will look the same as in the previous example

  • Related