> For the complete documentation index, see [llms.txt](https://larhues-personal-organization.gitbook.io/intro-to-data-visualization/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://larhues-personal-organization.gitbook.io/intro-to-data-visualization/getting-started/how-to-check-data-type.md).

# How to Check Data Type

In Pandas, you can check the data type of a column to determine if it is numeric or string (or any other type). This is useful for ensuring that operations are performed on the correct data types.

***

#### Checking Data Types with `dtype`

You can use the `dtype` attribute to inspect the data type of a column:

```
import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Los Angeles', 'Chicago']
}

df = pd.DataFrame(data)

# Check if a column is numeric
print(df['Age'].dtype)
# Check if a column is string (object type in Pandas)
print(df['Name'].dtype)
```

Output:

```
int64
object
```

***

#### Using `pd.api.types` for Type Checking

Pandas provides utility functions in `pd.api.types` to check for specific data types:&#x20;

pes:

```
from pandas.api.types import is_numeric_dtype, is_string_dtype

# Check if the 'Age' column is numeric
print(is_numeric_dtype(df['Age']))

# Check if the 'Name' column is a string
print(is_string_dtype(df['Name']))
```

Output:

```
True
True
```

#### Use Cases

1. **Data Validation**: Ensure columns contain the expected data type before performing operations.
2. **Conditional Logic**: Apply different logic based on the column's data type.

   Example:

   ```
   def process_column(column):
       if is_numeric_dtype(column):
           return column.mean()  # Calculate the mean for numeric columns
       elif is_string_dtype(column):
           return column.value_counts()  # Get value counts for string columns

   print(process_column(df['Age']))
   print(process_column(df['Name']))
   ```

   Output:

   ```
   30.0
   Alice      1
   Bob        1
   Charlie    1
   dtype: int64
   ```

   By checking the data type of columns, you can write robust and flexible code that handles different types of data effectively.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://larhues-personal-organization.gitbook.io/intro-to-data-visualization/getting-started/how-to-check-data-type.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
