Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Classify URLs
#6
Hi again,

A variable is just a name to which you can assign values or other objects. df_filtered is a variable of type pandas.DataFrame.

As to the issue at hand, df_filtered appears to me like it should be defined. I ran a quick test at home and didn't get the error message you are obtaining:
import pandas as pd
# Setting up the mock dataframe
df = pd.DataFrame({
    'Destination': ['website.com/test1', 'www.website.com/test2', 'www.somethingelse.com'], 
    'Source': ['website.com/string1', 'website.com/string2', 'website.com/somethingelse'], 
    'Type': ['AHREF', 'AHREF', 'AHREF']
})

# Simplifying your code to filter the DataFrame
# Note that the change from df[] to df.loc[] is to prevent some
# issues you needn't worry about right now
# If you do want, you can look it up here:  
# http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
df_filtered = df.loc[(df.Destination.str.contains("website.com")) & 
    (df.Source.str.contains("website.com")) & 
    (df.Type == "AHREF"), :]

def categories(Source):
    if '/string1' in Source: return 'Category 1'
    elif '/string2' in Source: return 'Category 2'
    else: return 'other'

df_filtered.loc[:, 'Category'] = df_filtered.Source.apply(categories)
print(df_filtered)
Output:
Destination Source Type Category 0 website.com/test1 website.com/string1 AHREF Category1 1 www.website.com/test2 website.com/string2 AHREF Category2
Did you run all of your code in consecutively, in the order shown in your post ? If you copy and paste my code in this current post, do you get the same error message ?

As for your other question on remembering how to complete some of those tasks, I personally learned by practicing a lot. Follow some tutorials / books, try the codes yourself, play with them, modify them, etc. Try to read the documentation on some libraries you find interesting. You'll learn about functions you may need but didn't know existed or maybe you'll just keep in the back of your head that function xyz exists and can be used in scenario abc.

Cheers
Reply


Messages In This Thread
Classify URLs - by Newlearner - Aug-11-2019, 11:02 AM
RE: Classify URLs - by boring_accountant - Aug-11-2019, 04:54 PM
RE: Classify URLs - by Newlearner - Aug-11-2019, 06:32 PM
RE: Classify URLs - by boring_accountant - Aug-11-2019, 08:27 PM
RE: Classify URLs - by Newlearner - Aug-12-2019, 07:40 PM
RE: Classify URLs - by boring_accountant - Aug-13-2019, 12:18 AM
RE: Classify URLs - by Newlearner - Aug-13-2019, 05:58 PM
RE: Classify URLs - by boring_accountant - Aug-14-2019, 02:43 AM
RE: Classify URLs - by Newlearner - Aug-14-2019, 11:39 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a Python text mining script to classify text with multiple classifications? Endearment 0 1,871 Oct-21-2019, 07:50 PM
Last Post: Endearment

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020