# Python Tips - Playing with Pandas

DevFeed: [Python Tips - Playing with Pandas](<https://devfeed.tech/articles/python-tips-playing-with-pandas-28229.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/python/2020/01/06/python-tips-playing-with-pandas.html>)

Author: Fuzzygroup

Published: 2020-01-06T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [pandas](<https://devfeed.tech/topics/pandas.md>), [Python](<https://devfeed.tech/topics/python.md>), [data-processing](<https://devfeed.tech/topics/data-processing.md>), [dataset](<https://devfeed.tech/topics/dataset.md>), [JSON](<https://devfeed.tech/topics/json.md>), [CSV](<https://devfeed.tech/topics/csv.md>)

Tags: [csv](<https://devfeed.tech/tags/csv.md>), [data](<https://devfeed.tech/tags/data.md>), [data-processing](<https://devfeed.tech/tags/data-processing.md>), [dataset](<https://devfeed.tech/tags/dataset.md>), [json](<https://devfeed.tech/tags/json.md>), [library](<https://devfeed.tech/tags/library.md>), [pandas](<https://devfeed.tech/tags/pandas.md>), [python](<https://devfeed.tech/tags/python.md>), [script](<https://devfeed.tech/tags/script.md>)

## AI overview

A practical introduction to using pandas in Python for loading and inspecting a dataset. The article demonstrates reading JSON data, sampling and copying a DataFrame, checking its length and type, examining columns, accessing text values, iterating over them, and converting values to strings.

## Source excerpt

As I write this it is 3:45 am and I am playing with Python. I have a small script that looks like this: import pandas as pd DATA_PATH = "data/" filename = "file.json" train_sample_size = 45000 dataset = pd.read_json(os.path.join(DATA_PATH, filename), lines=True) dataset_test = dataset.copy().sample(train_sample_size).reset_index() dataset_head = dataset_test.head(train_sample_size) pdb.set_trace() The pandas library give you a bunch of facilities for looking at data and organizing it into what are called data frames. If you think of pandas as a spreadsheet that you can use programmatically, well, that's not far off. Pandas is widely used for Python data processing and can read data from CSV and JSON formats (among others). If I want to investigate the length of a variable, I can do this: (Pdb) len(dataset) 45648 And if I want to know the type of a variable, I can do this: type(dataset) <class 'pandas.core.frame.DataFrame'> And if I want to know the columns that Pandas as loaded from the json, I can do this: dataset.columns Index(['tweet_id', 'timestamp_us_eastern', 'author_handle', 'author_screenname', 'authority', 'predicted_gender', 'predicted_language', 'predicted_country', 'predicted_state_or_province', 'predicted_city', 'given_location', 'followers', 'following', 'predicted_sentiment', 'context_notes', 'antisemitic_classification', 'author_bio', 'text', 'is_truncated', 'is_retweet', 'lang', 'Majority Model', 'label'], dtype='object') What this tells me is that I have a dataset variable with a text column that I can access by dataset.text. And then if I want the first 'row' of data in this column, I can do this: (Pdb) dataset.text[0] "Jerry Seinfeld spent a lot of time in Israel's apartheid State supporting the Zionists and giving the bastards credibility. Think its fair to shun his crappy unfunny TV Shows. Let him know what people think of him brown nosing the terrorists of Israel.\n" So [0] gives the first bit of data. And, correspondingly, [1] gives the next