1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# Importing Scientific Data using Pandas
[Introduction text]
Pandas is a library that is built on top of NumPy and is therefore customarily to import both when working with Pandas.
```python
import numpy as np
import pandas as pd
```
---
DataFrame
A `DataFrame` in pandas is a two-dimensional data structure like table with rows and columns. We can either load some data in a [python dictionary:](https://www.w3schools.com/python/python_dictionaries.asp)
```python
# Tensile test data
data = {
"Force (N)": [10, 15, 20, 25],
"Displacement (mm)": [1.2, 1.8, 2.5, 3.0],
"Stress (MPa)": [2.6, 3.9, 5.2, 6.5],
}
```
Or we can load a spreadsheet in the form of a CSV file with the `read_csv()` function. In order to do this we need
```python
# Read CSV file into a DataFrame
pd.read_csv("data.csv")
```
---
Alternatively, the `read_excel()` function allows you to import xlsx files. For this you need to specify the sheet name.
```python
df.read_excel("foo.xlsx", sheet_name="Sheet1", index_col=None, na_values=["NA"])
```
---
Object Creation
Up to this point, the data has been read into memory but not assigned to a variable. By convention, we will assign the resulting DataFrame to the variable `df`, where the name `df` serves as a common shorthand for “data frame.”
```python
# Create DataFrame with row labels
df = pd.DataFrame(data, index=["Test1", "Test2", "Test3", "Test4"])
print(df)
```
Creating the object allows us to re-call the data or specific data points if needed.
---
Calling Data
Data can be called using the square brackets. We can get an entire column or row using it's label as follows:
Single point:
```python
df[3,2]
```
Ranges:
```python
df[2:4,2:3]
```
Data series by label
```python
df["Test1"]
```
---
Assignment 1:
```python
```
---
Assignment 2:
Load a the data from the csv file and calculate the mean point of the array.
```python
```
|