Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Charles_ContextEffectsOnAbstract_tweets/whitespace_location_cleared.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
45 lines (31 sloc)
1.11 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string | |
import re | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from collections import Counter | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import re | |
#read file | |
df = pd.read_csv(r"clean_file.csv", encoding ="latin-1") | |
cities_df = pd.read_csv(r"uscitiesv1.5.csv", encoding ="latin-1") | |
nan_value = float("NaN") | |
cities_df["full_county"] = (cities_df["county_name"] + ", " + cities_df["state_id"]).astype(str).str.lower() | |
cities_df["full_city"] = (cities_df["city"] + ", " + cities_df["state_id"]).astype(str).str.lower() | |
full_city = cities_df["full_city"].tolist() | |
full_county = cities_df["full_county"].tolist() | |
df["location"] = df["location"].astype(str).str.lower() | |
def filter_location(word): | |
if (word in full_county): | |
return word | |
elif (word in full_city): | |
return word | |
else: | |
return nan_value | |
df['location'] = df['location'].apply(lambda x: filter_location(x)) | |
df = df.dropna(subset=['location']) | |
df = df.reset_index(drop=True) | |
df['tweet'] = df['tweet'].apply(lambda x: " ".join(str(x).split())) | |
df.to_csv("clean_file_test.csv") | |
df.head() | |