df = pd.read_csv('tweets.csv')
result = filter(lambda x: x[0:2]=='RT', tweets_df['text']) # Select retweets from the Twitter DataFrame
res_list = list(result) # Create list from filter object result
for tweet in res_list:
print(tweet)
import pandas as pd
df = pd.read_csv('tweets.csv')
def count_entries(df, col_name='lang'):
"""Return a dictionary with counts of occurrences as value for each key."""
cols_count = {} # Initialize an empty dictionary
# Add try block
try:
col = df[col_name] # Extract column from DataFrame
# Iterate over the column in dataframe
for entry in col:
if entry in cols_count.keys(): # If entry is in cols_count, add 1
cols_count[entry] += 1
else:
cols_count[entry] = 1
return cols_count
# Add except block
except:
print('The DataFrame does not have a ' + col_name + ' column.')
result1 = count_entries(tweets_df, 'lang')
print(result1)
import pandas as pd
df = pd.read_csv('tweets.csv')
def count_entries(df, col_name='lang'):
"""Return a dictionary with counts of occurrences as value for each key."""
# Raise a ValueError if col_name is NOT in DataFrame
if col_name not in df.columns:
raise ValueError('The DataFrame does not have a ' + col_name + ' column.')
cols_count = {} # Initialize an empty dictionary
col = df[col_name] # Extract column from DataFrame
# Iterate over the column in DataFrame
for entry in col:
if entry in cols_count.keys(): # If entry is in cols_count, add 1
cols_count[entry] += 1
else:
cols_count[entry] = 1
return cols_count
result1 = count_entries(tweets_df, 'lang')
print(result1)