This file contains hidden or 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
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 6 | 7 | 8 | 9 | 10 |
This file contains hidden or 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 pandas as pd | |
| # Prefix all the default column labels with column_. | |
| df = pd.read_csv('read_csv_prefix.csv', header=None, prefix='column_') |
This file contains hidden or 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
| col0 | col1 | col2 | col3 | col4 | col5 | |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 |
This file contains hidden or 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 pandas as pd | |
| # From our CSV we only want to read columns 1 & 2. | |
| df = pd.read_csv('read_csv_usecols.csv', usecols=['col1', 'col3']) |
This file contains hidden or 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
| col0 | col1 | |
|---|---|---|
| Dean | 70 |
This file contains hidden or 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 pandas as pd | |
| # Cast columns 0 & 1 to name & age, | |
| df = pd.read_csv('read_csv_names.csv', header=0, names=['name', 'age']) |
This file contains hidden or 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 pandas as pd | |
| df = pd.read_csv('../test_data/dummy_file.csv') |
This file contains hidden or 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
| # pandas.read_csv | |
| # Source: https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html#pandas.read_csv | |
| pandas.read_csv(filepath_or_buffer, sep=',', delimiter=None, header='infer', names=None, index_col=None, usecols=None, | |
| squeeze=False, prefix=None, mangle_dupe_cols=True, dtype=None, engine=None, converters=None, | |
| true_values=None, false_values=None, skipinitialspace=False, skiprows=None, skipfooter=0, nrows=None, | |
| na_values=None, keep_default_na=True, na_filter=True, verbose=False, skip_blank_lines=True, | |
| parse_dates=False, infer_datetime_format=False, keep_date_col=False, date_parser=None, dayfirst=False, | |
| cache_dates=True, iterator=False, chunksize=None, compression='infer', thousands=None, decimal='.', | |
| lineterminator=None, quotechar='"', quoting=0, doublequote=True, escapechar=None, comment=None, | |
| encoding=None, dialect=None, error_bad_lines=True, warn_bad_lines=True, delim_whitespace=False, |
This file contains hidden or 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 pandas as pd | |
| if __name__ == '__main__': | |
| # Create a DataFrame with dummy data. | |
| df = pd.DataFrame(data={'staff_no': [9999] * 5, | |
| 'name': ['Dean McGrath'] * 5, | |
| 'year': [2016, 2017, 2018, 2019, 2020], | |
| 'hours': [349, 231, 876, 679, 976]}) | |
| # Pivot the DataFrame based on Staff Number & Employee Name. |
This file contains hidden or 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
| # Unpivot the original DataFrame. | |
| df = df.melt(id_vars=['staff_no', 'name'], var_name='year', | |
| value_name='hours') |