There is a DF with column Views, which contains lists of dates. I need to count not-empty rows of this DF, ie rows where Views:= [1970-01-01 00:00:00] (type: list of datetimes)
What I tried:
a = datetime.strptime('1970-01-01 00:00:00', '%Y-%m-%d %H:%M:%S')
result.loc[result.Views[0] != a ]
result.loc[result.Views[0][0] != a ]
result.loc[result.Views[0][0] != [a] ]
result.loc[result.Views[0] != [a] ]
also tried isin, unique funcs, but all it gives either KeyValue error or 'list not hashable' error
Please, help
UPDATE
The code that works:
a = datetime.strptime('1970-01-01 00:00:00', '%Y-%m-%d %H:%M:%S')
test = result.explode('Views')
out = test[test.Views != a]
result['Count'] = result.Views.apply(lambda x: sum(y != a for y in x))
viewed = len(result.loc[result.Count > 0]) #Total rows with not empty views
But I suspect there is a simpler and quicker way to count such stuff. And nevertheless, how can I achieve items in lists by their index, if this list is a value in DF? - still unanswered
UPDATE
Shortest solution:
print(len(df.loc[df["Views"].apply(lambda l: pd.Series(l).explode().ne("1970-01-01").all())]))
Use DataFrame.explode
for convert lists to scalars - flatten, so possible compare:
df = result.explode('Views')
a = datetime.strptime('1970-01-01 00:00:00', '%Y-%m-%d %H:%M:%S')
out = df[df.Views != a ]
If need count values use generator with sum
:
result['count'] = result.Views.apply(lambda x: sum(y != a for y in x))
explode()
and logical operator as mask import numpy as np
import pandas as pd
import random
df = pd.DataFrame(
{
"ObjectClass": np.repeat("flats", 20),
"Views": [
np.random.choice([pd.to_datetime("1-jan-1970")]+list(pd.date_range("1-aug-2021", periods=3)), random.randint(1,3)) for i in range(20)
],
}
)
print(len(df.loc[df["Views"].apply(lambda l: pd.Series(l).explode().ne("1970-01-01").all())]))
df
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.