Intermediate Python Flashcards
Advanced level python
How do you combine multiple .csv files into a single DataFrame?
Loop through each CSV file in the directory
Directory where the individual CSV files are saved
csv_directory = ‘/Users/Documents/Udacity/Political_News_2024/political_news’
Initialize an empty list to hold DataFrames
dataframes = []
for filename in os.listdir(csv_directory):
if filename.endswith(‘.csv’): # Check if the file is a CSV
file_path = os.path.join(csv_directory, filename)
df = pd.read_csv(file_path) # Save the CSV file
dataframes.append(df) # Append the DataFrame to the list
# Concatenate all DataFrames in the list
combined_news_df = pd.concat(dataframes, ignore_index=True)
# Get today’s date in YYYY-MM-DD format
today = datetime.today().strftime(‘%Y-%m-%d’)
# Define the path and name for the combined CSV file
combined_filename = os.path.join(csv_directory, f’combined_political_news_data_{today}.csv’)
# Save the combined DataFrame to CSV
combined_news_df.to_csv(combined_filename, index=False)
What are the steps to learning python
Learn the python language
Learn the standard library
Learn the ecosystem
What three questions are asked to verify if data is a collection?
Can I know the size, or length, of this data? If so, the data is Sized.
Can I produce elements from this data set, one at a time? If so, the data is Iterable.
Can I check whether an element is in this data set? If so, the data is a Container.
What questions should you ask when thinking about structure, performance, and clarity?
Is the data ordered (sequential), or unordered?
Is the data associative, mapping between keys and values?
Is the data unique?
Define mutability
whether an object in Python can be changed after it has been created.
Define hashability
If all data in a collection is immutable. An object is hashable if it has a fixed hash value for its lifetime and it implements the __hash__() method.
What does false mean in Python
Something that is False
What means nothingness?
None
Iterable
An object that can produce a stream of elements of data.
Mapping
An unordered collection associating elements of data (keys) to other elements of data (values).
Set
An unordered collection of unique data elements
Sized
An object that has a finite size
Structured data
Information to which we assign meaning through organizaiton
What is a bool
Either False or True and encodes binary values
What are the numeric values for a number?
int (integer) or float (real)
What is a string
An immutable sequence of text characters
What represents nothingness
None
What are the building blocks of data
bool
number
string
None
How do you escape special characters in string literals
\
print(‘doesn't’)
What does r”U\escaped” do?
It avoids ecaping, creating a raw string so special sequences U\escaped, \n (newline), or \t (tab) are treated as literal text, not as control characters.
name common methods for Strings
- greeting.find(‘lo’) # 3 (-1 if not found)
- greeting.replace(‘llo’, ‘y’) # => “Hey world!”
- greeting.startswith(‘Hell’) # => True
- greeting.isalpha() # => False (due to ‘!’)
- greeting.lower() # => “hello world! “
- greeting.title() # => “Hello World! “
- greeting.upper() # => “HELLO WORLD! “
- greeting.strip(‘dH !’) # => “ello worl”
Visualize how to convert the following:
str
int
float
bool
str(42) # => “42”
int(“42”) # => 42
float(“2.5”) # => 2.5
bool(None)
T or F: converting a value to a bool turns it into a boolean
F: it gives the value an essence of truthiness
bool(None) # => False
bool(False) # => False
bool(41) # => True
bool(‘abc’) # => True
What is a Python Object
A Python object can be thought of as a suitcase that has a type and contains data about its value. Everything in Python is an object.
isinstance(4, object) # => True
isinstance(“Hello”, object) # => True