Getting a value from a DataFrames column based on the condition (if at least one value != nan)

Viewed 39

I have a data frame containing user behaviour on a website whereby every row is a single action performed by this concrete user (scroll, click, pageview). It means that for one unique user I have a different amount of rows where data such as e.g. city or device is always the same, but the URLs visited are different. But for some reason even though the data was collected within one session (= within a couple of minutes from one user's device) some of the rows have missing values (nans). enter image description here My goal is to create user profiles from that, but I don't know how to tell python to look through the language column for example and even if at least one value is not nan, then it should write this value as this user's language. I know it's nothing complicated but I can't figure it out and would be very thankful for your help.

UPD: the whole data frame has about 7. Mio rows containing actions from about 400 Tsd. users so considering memory usage would be great too

1 Answers

Nevermind, I kinda found a solution. Posting it here as the post had a couple of upvotes.

test = df.ga_city  # series from column
list = []  # list to store user's city

for index, value in test.iteritems():
    if pd.notna(value) and value not in list:
        list.append(value)
Related