-1
df = {'year': [2018, 2018, 2017, 2019],
'time': [12, 8, 10, 10],
'weekday': [4, 6, 5, 1]}
df = pd.DataFrame(df)
df

if df['weekday'] <5:
       df['weekend'] = df['weekday'].bool()
else:
       df['weekend'] = df['weekday'].bool()

I want the output that looks like the image below enter image description here

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Please include a _small_ subset of your data as a __copyable__ piece of code that can be used for testing as well as your expected output for the __provided__ data. See [MRE - Minimal, Reproducible, Example](https://stackoverflow.com/help/minimal-reproducible-example), and [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/15497888) for more information. – Henry Ecker Aug 21 '21 at 20:14
  • Can you also elaborate on what your desired output looks like, given your sample data. Also, can you and elaborate what your issue since, as written, your question Title makes no sense, and there doesn't seem to be any other hint as to what your question might really be. – itprorh66 Aug 21 '21 at 20:22
  • Not a `machine-learning` question, kindly do not spam irrelevant tags (removed). – desertnaut Aug 22 '21 at 22:38

2 Answers2

1

To answer the question, a numpy.where function will be precise

# df['weekend'] = np.where(df['weekday']>4, True, False)
df['weekend'] = np.where(df['weekday']>4, 1, 0)
df.head()
1

Try this:

df["weekend"] = 0
df.loc[df["weekday"] >= 5, "weekend"] = 1
woblob
  • 1,349
  • 9
  • 13