A data frame is a two-dimensional, labeled grid for manipulating tabular data in memory. In pandas, it can hold different kinds of values and offers column operations that make selection and cleaning more concise than working with nested lists. section 24.2.3 section 24.3.1
Create a pandas data frame from a list of lists. Rows and columns initially have numeric labels; the columns argument supplies more useful names. section 24.2.3
df = pd.DataFrame(grid, columns=["one", "two", "three"] ) print(df) one two three 0 1 2 3 1 4 5 6 2 7 8 9
The named columns make the grid easier to read and select from. section 24.2.3
Select and operate on columns:
- df["two"] selects one column, whose values can be iterated over. section 24.2.3
- df[["one", "three"]] selects multiple columns and returns another data frame. section 24.2.3
- edges.add(2) adds 2 to every value in the selected numeric frame. section 24.2.3
Load data with pd.read_csv() or pd.read_json(). For CSV input, na_values can turn markers such as "Missing" into NaN. Save a frame with methods such as to_csv() and to_json(); to_csv(..., index=False) omits the row index. section 24.3.1 section 24.3.1
Clean a percentage column
For a temperature frame temp, temp[17] = pd.to_numeric(temp[17].str.strip("%")).div(100) removes percent signs, converts the strings to numbers, and expresses percentages as fractions. For example, "0.00%" becomes 0.0. section 24.3.2
Combine related frames with pd.merge(). Given calls and revenue frames, pd.merge(calls, revenue, on=['Territory', 'Month']) creates a frame joining their columns on the two shared fields. Merges can also handle one-to-many and many-to-many relationships, and left and right joins. section 24.4.1
Filter rows by placing a condition inside brackets. For example, calls_revenue[calls_revenue.Territory == 3] keeps rows for territory 3. A calculation can also become a new column: calls_revenue['Call_Amount'] = calls_revenue.Amount / calls_revenue.Calls. section 24.4.2
Pandas may not be the best choice for datasets that are poorly suited to it or require near-real-time processing. Some input operations also depend on other libraries, which may be the source of errors. section 24.7 section 24.3.1


















