My DataFrame looks like this:
Category Date
81 Monate 2020-01-01
88 Monate 2020-01-02
58 Monate 2020-01-03
3 Monate 2020-01-04
23 Monate 2020-01-05
.. ... ...
134 Wochen 2020-05-24
145 Tage 2020-05-25
147 Tage 2020-05-26
146 Tage 2020-05-27
148 Tage 2020-05-28
It is ordered by Date
. I need to run a check if on each row Monate follows Monate, Wochen follows Wochen and so on. It is allowed that Wochen follows Monate and Tage follows Wochen. I hope it is clear that I mean. Something looks this should cause an error, since the sequence is invalid.
Category Date
81 Monate 2020-01-01
88 Monate 2020-01-02
58 Tage 2020-01-03
3 Monate 2020-01-04
23 Monate 2020-01-05
.. ... ...
134 Wochen 2020-05-24
145 Tage 2020-05-25
147 Tage 2020-05-26
146 Wochen 2020-05-27
148 Tage 2020-05-28
I could try to write a pretty complicated and probably slow iteration over each row.
for row in result_df.iterrows():
do xyz
Is there a better and quicker way to check for an ongoing sequence in a Series or a maybe in a list, dictionary etc.?
I believe you can create a numeric dictionary stating the order and replace the values of the Category column and check if series.diff
is never negative with series.all
:
def check(dataframe):
d = {'Monate':1,'Wochen':2,'Tage':3}
return dataframe['Category'].replace(d).diff().fillna(0).ge(0).all()
Test Runs:
print(df,'\n\n',f"Valid? : {check(df)}",'\n\n',df1,'\n\n',f"Valid? : {check(df1)}")
Category Date
81 Monate 2020-01-01
88 Monate 2020-01-02
58 Monate 2020-01-03
3 Monate 2020-01-04
23 Monate 2020-01-05
134 Wochen 2020-05-24
145 Tage 2020-05-25
147 Tage 2020-05-26
146 Tage 2020-05-27
148 Tage 2020-05-28
Valid? : True
Category Date
81 Monate 2020-01-01
88 Monate 2020-01-02
58 Tage 2020-01-03
3 Monate 2020-01-04
23 Monate 2020-01-05
134 Wochen 2020-05-24
145 Tage 2020-05-25
147 Tage 2020-05-26
146 Wochen 2020-05-27
148 Tage 2020-05-28
Valid? : False
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.