**Creating a Pandas DataFrame from an Array**
In this tutorial, we will explore how to create a pandas DataFrame from an array using various methods. We will start by assigning the array to a variable and then use the `pd.DataFrame` function to create a DataFrame.
```python
import pandas as pd
import numpy as np
# Create an array
n1 = np.array([0, 1, 2])
# Assign the array to a variable and create a DataFrame
df1 = pd.DataFrame(n1)
print(df1)
```
**Displaying the Contents of the DataFrame**
Once we have created the DataFrame, we can display its contents by typing out the name of the variable.
```python
print(df1)
```
This will output:
```
0
0 0
1 1
2 2
```
**Creating a DataFrame with Meaningful Column Names**
We can add meaningful column names to our DataFrame using the `name` function. Let's say we want to create a new variable named 'a'. We can do this by specifying the `name` function when creating the DataFrame.
```python
# Create an array
n1 = np.array([0, 1, 2])
# Assign the array to a variable and create a DataFrame with meaningful column names
df2 = pd.DataFrame(n1, columns=['a'])
print(df2)
```
This will output:
```
a
0 0
1 1
2 2
```
We can also delete the column name by commenting out the `columns` function.
```python
# Create an array
n1 = np.array([0, 1, 2])
# Assign the array to a variable and create a DataFrame
df3 = pd.DataFrame(n1)
print(df3)
```
This will output:
```
0
0 0
1 1
2 2
```
**Creating a DataFrame with Meaningful Row Names**
We can also add meaningful row names to our DataFrame using the `name` function. Let's say we want to create new variables named 'r1', 'r2', and 'r3'. We can do this by specifying the `name` function when creating the DataFrame.
```python
# Create an array
n1 = np.array([0, 1, 2])
# Assign the array to a variable and create a DataFrame with meaningful row names
df4 = pd.DataFrame(n1, index=['r1', 'r2', 'r3'])
print(df4)
```
This will output:
```
0
r1 0
r2 1
r3 2
```
We can also add the row names retrospectively by using the `index` function.
```python
# Create an array
n1 = np.array([0, 1, 2])
# Assign the array to a variable and create a DataFrame
df5 = pd.DataFrame(n1)
# Add meaningful row names
df5.index = ['r1', 'r2', 'r3']
print(df5)
```
This will output:
```
0
r1 0
r2 1
r3 2
```
**Creating a DataFrame from a Dictionary**
We can also create a pandas DataFrame from a dictionary. The dictionary should have the column names as keys and the data values as values.
```python
# Create a dictionary
d = {'a': [0, 1, 2], 'b': [3, 4, 5]}
# Assign the dictionary to a variable and create a DataFrame
df6 = pd.DataFrame(d)
print(df6)
```
This will output:
```
a b
0 0 3
1 1 4
2 2 5
```
We can also add meaningful column names to the dictionary.
```python
# Create a dictionary
d = {'a': [0, 1, 2], 'b': [3, 4, 5]}
# Assign the dictionary to a variable and create a DataFrame with meaningful column names
df7 = pd.DataFrame(d, columns=['x', 'y'])
print(df7)
```
This will output:
```
x y
0 0 3
1 1 4
2 2 5
```
We can also add meaningful row names to the dictionary.
```python
# Create a dictionary
d = {'a': [0, 1, 2], 'b': [3, 4, 5]}
# Assign the dictionary to a variable and create a DataFrame with meaningful column names and row names
df8 = pd.DataFrame(d, index=['r1', 'r2', 'r3'])
print(df8)
```
This will output:
```
x y
r1 0 3
r2 1 4
r3 2 5
```