- Given a dataset
df = pd.DataFrame({"Runner": ["Runner1", "Runner2", "Runner3", "Runner4"], "distance":[2,1,0,1]})
- I first remove the 0s.
df = df[df["Runner"] > 0]
- I assign df["distance] as the parameter data and df["Runner] as parameter index of a new Series ser1
ser1 = pd.Series(data=df["distance"], index=df[df["Runner"])
Here is how ser1 looks -
Runner
Runner1 NaN
Runner2 NaN
Runner4 NaN
Name: distance, dtype: float64
I expected ser1 to show
Runner
Runner1 2
Runner2 1
Runner4 1
Name: distance, dtype: int64
CodePudding user response:
You need to use:
df = df[df["distance"] > 0]
ser1 = pd.Series(data=df["distance"].values, index=df["Runner"])
But the ideal way to do it is:
ser1 = df[df["distance"] > 0].set_index('Runner')['distance']
Output:
Runner
Runner1 2
Runner2 1
Runner4 1
Name: distance, dtype: int64
CodePudding user response:
To remove the zero's use
df = df[df["distance"] > 0]
then
df['distance'].index = df['Runner']
ser1 = df['distance']
- output
Runner
Runner1 2
Runner2 1
Runner4 1
Name: distance, dtype: int64