python pandas

>>> df2 = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
...                    columns=['a', 'b', 'c'])
>>> df2
   a  b  c
0  1  2  3
1  4  5  6
2  7  8  9

Here is what the above code is Doing:
1. We’re creating a DataFrame from a NumPy array.
2. We’re specifying the column names.
3. We’re displaying the DataFrame.

The output is a DataFrame that looks like this:

a b c
0 1 2 3
1 4 5 6
2 7 8 9

You can also create a DataFrame from a Python dictionary.

Here’s an example:

>>> df3 = pd.DataFrame({‘Name’: [‘John’, ‘Paul’, ‘Ringo’],
… ‘Age’: [21, 22, 23]})
>>> df3
Age Name
0 21 John
1 22 Paul
2 23 Ringo

Here’s what the above code is doing:
1. We’re creating a DataFrame from a dictionary.
2. We’re specifying the column names.
3. We’re displaying the DataFrame.

The output is a DataFrame that looks like this:

Age Name
0 21 John
1 22 Paul
2 23 Ringo

You can also create a DataFrame from a list of lists.

Here’s an example:

>>> df4 = pd.DataFrame([[‘John’, 21], [‘Paul’, 22], [‘Ringo’, 23]],
… columns=[‘Name’, ‘Age’])
>>> df4
Name Age
0 John 21
1 Paul 22
2 Ringo 23

Here’s what the above code is doing:
1. We’re creating a DataFrame from a list of lists.
2. We’re specifying the column names.
3. We’re displaying the DataFrame.

The output is a DataFrame that looks like this:

Name Age
0 John 21
1 Paul 22
2 Ringo 23

You can also create a DataFrame from a CSV file.

Here’s an example:

>>> df5 = pd.read_csv(‘data.csv’)
>>> df5
Name Age
0 John 21
1 Paul 22
2 Ringo 23

Here’s what the above code is doing:
1. We’re creating a DataFrame from a CSV file.
2. We’re specifying the column names.
3. We’re displaying the DataFrame.

The output is a DataFrame that looks like this:

Name Age
0 John 21
1 Paul 22
2 Ringo 23

You can also create a DataFrame from a SQL database.

Here’s an example:

>>> df6 = pd.read_sql(‘SELECT * FROM data’, con=conn)
>>> df6
Name Age
0 John 21
1 Paul 22
2 Ringo 23

Here’s what the above code is doing:
1. We’re creating a DataFrame from a SQL database.
2. We’re specifying the column names.
3. We’re displaying the DataFrame.

The output is a DataFrame that looks like this:

Name Age
0 John 21
1 Paul 22
2 Ringo 23

You can also create a DataFrame from a JSON file.

Here’s an example:

>>> df7 = pd.read_json(‘data.json’)
>>> df7
Name Age
0 John 21
1 Paul 22
2 Ringo 23

Here’s what the above code is doing:
1. We’re creating a DataFrame from a JSON file.
2. We’re specifying the column names.
3. We’re displaying the DataFrame.

The output is a DataFrame that looks like this:

Name Age
0 John 21
1 Paul 22
2 Ringo 23

You can also create a DataFrame from an Excel file.

Here’s an example:

>>> df8 = pd.read_excel(‘data.xlsx’)
>>> df8
Name Age
0 John 21
1 Paul 22
2 Ringo 23

Here’s what the above code is doing:
1. We’re creating a DataFrame from an Excel file.
2. We’re specifying the column names.
3. We’re displaying the DataFrame.

The output is a DataFrame that looks like this:

Name