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:
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:
Any advice? Thanks!
Let's say I have this dataframe:
1 2 3 4 5 6 7 8 9 10 11 12 |
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) |
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:
1 2 3 4 5 6 7 8 9 10 |
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) |