Python Tutorial: Building DataFrames from scratch

Learn to build pandas DataFrames from scratch using dictionaries, lists, and the zip function. The video covers broadcasting, where assigning a single value to a new column automatically fills every row, plus how to rename columns and indices by reassigning the .columns and .index attributes.
Chapters
We've seen how to work with data frames in memory, but how do we get them in memory? In the intermediate Python for Data Science course, we used readcsv to load a dataframe from a comma separated values file. For instance, here we use a file users CSV to create a data frame called users. The file records visitors to a blog for a band and who signed up for a newsletter. By tracking where visitors come from, this information can help design tools later. Data frames can also be rolled by hand using dictionaries. Remember, dictionaries or associative arrays are a core data structure in Python. Here we construct a dictionary of lists with the same user's data. The keys of the dictionary data are used as column labels. Notice with no index specified. The row labels of The DataFrame are integers 0 to 3 by default. Let's build the dataframe users up in a different way using conforming lists Cities sign ups visitors weekdays for the column data. It is useful to be able to build data frames from lists. Because lists are a common data structure in Python, it's natural that we might receive data accumulated in lists. We can then define two other list labels containing the column labels and list cols containing the column entries for each column. Notice listcols is a list of lists using Python's list and zip functions. Construct a list called zipped of tuples, column names and columns to feed to the dict command. Calling DICTZips creates a DICT data which is then used with PD DataFrame to build the dataframe, let's look again at broadcasting a convenient technique in numpy and pandas. With the dataframe users in memory, a new column say fees can be created on the fly by using the new column label fees and by assigning the scalar value 0, the value is broadcast to the entire column. Broadcasting saves time in generating long lists, arrays or columns without loops. Broadcasting is not restricted to numbers. Here we create a dictionary data with column labels, height and sex as keys and a list and a single character string m as values. When the dict data is used to create dataframe results, the value m is broadcast to the entire column. Remember, we can change the column and index labels using the columns and index attributes of a pandas dataframe. We can assign lists of strings to the attributes, columns and index as long as they are of suitable length, I.e. the number of columns and rows respectively. It's time for you to practice using other data frame construction techniques. Broadcasting and relabeling.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







