2

I have a pd like below:

"Date" is the index, not column

Date Value
2020-01-01 1
2020-01-02 2
2020-01-03 3
2020-01-04 4
2020-01-05 5

and I want to do something like if date > 2020-01-02 and date < 2020-01-05

then Value = 0

Date Value
2020-01-01 1
2020-01-02 2
2020-01-03 0
2020-01-04 0
2020-01-05 5

.loc can only trim the whole data set and I don't want that. May I know what can I do to achieve this? Thanks

2
  • What is pd and what is .loc? What have you done so far? Commented Sep 13, 2021 at 5:53
  • 1
    Does this answer your question? Select DataFrame rows between two dates Commented Sep 13, 2021 at 5:58

2 Answers 2

1

In case it could be useful, here's an option using df.loc:

df['Date'] = pd.to_datetime(df['Date'])
df['Value'].loc[(df['Date'] > "2020-01-02") & (df['Date'] < "2020-01-05")] = 0
Sign up to request clarification or add additional context in comments.

3 Comments

OP doesn't want to use .loc and hasn't specified the type of the datetime column
Sorry @Luke, I've edited the answer. You need to change the date column to datetime format before filtering
Hi, Im sorry there is no df['Date'] column, it is a datetime index, not a column.
1

If you want if...else Try this:

import pandas as pd

df = pd.DataFrame({
    'Date': ['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05'],
    'Value': [1,2,3,4,5]})

df['Value'] = [
    0 if (x > '2020-01-02') & (x <= '2020-01-04') else  df['Value'].iloc[idx]
    for idx, x in enumerate(df['Date'])
]
print(df)

Output:

         Date    Value
0  2020-01-01      1
1  2020-01-02      2
2  2020-01-03      0
3  2020-01-04      0
4  2020-01-05      5

Or you can use .loc like below:

import pandas as pd

df = pd.DataFrame({
    'Date': ['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05'],
    'Value': [1,2,3,4,5]})

df['Date'] = pd.to_datetime(df['Date'])  
df.loc[(df['Date'] > '2020-01-02') & (df['Date'] <= '2020-01-04'), 'Value'] = 0

Edit Base On Your Comment (if Date is index):

import pandas as pd

df = pd.DataFrame({
    'Date': ['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05'],
    'Value': [1,2,3,4,5]})

df.set_index('Date', inplace=True)
df

Output:

enter image description here

For the above DataFrame you can use df.reset_index() and like below:

df = df.reset_index()
df['Date'] = pd.to_datetime(df['Date'])  
df.loc[(df['Date'] > '2020-01-02') & (df['Date'] <= '2020-01-04'), 'Value'] = 0

With if...else:

df = df.reset_index()
df['Value'] = [
    0 if (x > '2020-01-02') & (x <= '2020-01-04') else  df['Value'].iloc[idx]
    for idx, x in enumerate(df['Date'])
]

6 Comments

Hi, sorry the "Date" is the index, not the column. so I think I cannot use the method as you.
@user3239756 edited answer , for this problem you can use df.reset_index()
Thanks I will try to use reset_index(), however, can I use if statement with it? I actually want to do a series of functions/validation more than just put a value.
@user3239756 edited answer again and add part if..else in the end of answer
Thanks, I got this error : '>' not supported between instances of 'Timestamp' and 'str' but I think the reset index is the first start
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.