Python tests a true / false condition on a column of a data frame and returns the result in a new column

I am very new to Python coding, so I am trying to understand some basics - any input is appreciated.

I have a list of weekly dates, and I'm trying to run the if statement on days, i.e. if the number of days is less than 7, create a column with coefficient x or create coefficient y - as shown in the table below:

    week        day check       factor
0   2017-01-08  8   False       x
1   2017-01-15  15  False       x
2   2017-01-22  22  False       x
3   2017-01-29  29  False       x
4   2017-02-05  5   True        y

I tried the code below:

if df['day'] <7 :
    factor=weeks['day']/7
else:
    ....

and received an error message:

ValueError: The truth value of a Series is ambiguous

, , , , true/false. /, , . ?

+4
2

"" datetime dtype, to_datetime, dt.day np.where:

In [47]:
df['week'] = pd.to_datetime(df['week'])
df['factor'] = np.where(df['week'].dt.day < 7, 'y', 'x')
df

Out[47]:
        week  day  check factor
0 2017-01-08    8  False      x
1 2017-01-15   15  False      x
2 2017-01-22   22  False      x
3 2017-01-29   29  False      x
4 2017-02-05    5   True      y
+6

apply.

:

df['factor'] = df.apply(lambda row: "y" if row['day'] < 7 else "x", axis=1)

(axis=1) day. 7, "y", "n".

():

    check  day        week factor
 0  False    8  2017-01-08      x
 1   True    5  2017-02-05      y

, "", :

msk = df['day'] < 7   # Series of True/False values based on condition
df[factor] = msk.replace([True, False], ['y', 'x']) #convert True/False to y/x
0

Source: https://habr.com/ru/post/1658705/


All Articles