Data Handling using Pandas - I
Combining, attributes and CSV files
Use concat, attributes, read_csv and to_csv.
Combining DataFrames. The book uses append() to add the rows of one DataFrame below another. But pandas 2.0 removed append(), so today use pd.concat([df1, df2]).
Attributes of a DataFrame: index (row labels), columns, dtypes (data type of each column), values, shape (a tuple: number of rows, number of columns), size (total number of values), T (the transpose: rows become columns), empty; and the methods head(n) and tail(n).
CSV files (comma-separated values) store tables as plain text, one row per line.
- pd.read_csv(file, sep=',', header=0) loads a CSV file into a DataFrame. The default separator is a comma; header=0 means the first line holds column names; names= sets your own column labels.
- df.to_csv(file, sep=',') saves a DataFrame. header=False leaves out the column labels, and index=False leaves out the row labels.
Example: df.to_csv('marks.txt', sep='#', header=False, index=False) writes lines like 88#92#79.
Why use Pandas rather than NumPy? A Series aligns data by label in operations, while NumPy arrays work only by position. Read with read_csv, write with to_csv.
Practise this lesson
Questions that get harder as you improve, a Tutor that helps without giving the answer away, and progress you can see. Free for every learner.
No account, no email address. Opens the real product.