May-03-2019, 10:41 AM
I am pretty new at using Pandas, so I was wondering if anyone could help me with the below.
Let's say I have this dataframe:
What I want to do is replace all the nan values of types == 'Cat' with the weights of the types== 'Dog' so that my data would look like this:
Let's say I have this dataframe:
import pandas as pd
import numpy as np
data = [
['Rabbit', 2, 2],
['Dog', 5, 5.5],
['Dog', 3, 2.8],
['Cat', np.nan, np.nan],
['Cat', np.nan, np.nan],
]
df = pd.DataFrame(columns=["Type", "Weight_April", "Weight_May"], data=data)With other columns for weights for all months up until January.What I want to do is replace all the nan values of types == 'Cat' with the weights of the types== 'Dog' so that my data would look like this:
import pandas as pd
import numpy as npdata = [
['Rabbit', 2, 2],
['Dog', 5, 5.5],
['Dog', 3, 2.8],
['Cat', 5, 5.5],
['Cat', 5, 2.8],
]
df = pd.DataFrame(columns=["Type", "Weight_April", "Weight_May"], data=data)Any advice? Thanks!
