|
| 1 | +# Pandas-30minutes For QA |
| 2 | + |
| 3 | +**What is Pandas?** |
| 4 | +> **Python lib to handle ***data***.** |
| 5 | +
|
| 6 | +## 1.Talking about data |
| 7 | + |
| 8 | +**What kind of data Pandas to handle:** |
| 9 | + |
| 10 | +- **DataFrame**: Tablular data;header/column/row data |
| 11 | +- **Series**: each column in a dataframe is series |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## 1.1 Examples for DataFrame and Series data |
| 16 | + |
| 17 | +1. **DataFrame**: |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +```python |
| 24 | +df_cr= pd.DataFrame({ |
| 25 | + "Name":["Braund, Mr. Owen Harris", |
| 26 | + "Allen, Mr. William Henry", |
| 27 | + "Bonnell, Miss. Elizabeth",], |
| 28 | + "Age": [22, 35, 58], |
| 29 | + "Sex": ["male", "male", "female"], |
| 30 | +}) |
| 31 | +print(df_cr) |
| 32 | +``` |
| 33 | +--- |
| 34 | + |
| 35 | +2. **Series**: each column in a dataframe is series |
| 36 | + |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +```python |
| 41 | +print(df_cr["Age"]) |
| 42 | +print(df_cr['Age'].max()) |
| 43 | +print(df_cr['Age'].min()) |
| 44 | +print(df_cr['Age'].mean()) |
| 45 | +``` |
| 46 | +--- |
| 47 | + |
| 48 | +## 2.Read and Write Tablular Data |
| 49 | +Tabluar data: |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +```python |
| 58 | + |
| 59 | +``` |
| 60 | + |
| 61 | + |
| 62 | +## 3. Select Subset of Dataframe |
| 63 | + |
| 64 | +What does subset mean? |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | + |
| 73 | +## 5. Create Plot |
| 74 | + |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## 6. Read Excel/CSV data to Python object |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +```python |
| 83 | +import pandas as pd |
| 84 | +from pydantic import BaseModel,Field,ConfigDict |
| 85 | +class UnitInfoRawModel(BaseModel): |
| 86 | + unit_name: str = Field("", alias='单位名') |
| 87 | + unit_symbol: str = Field("", alias='单位符号') |
| 88 | + unit_symbol_latex: str = Field("", alias='单位符号LaTex') |
| 89 | + unit_group_name: str = Field("", alias='单位组名称') |
| 90 | + base_unit: str = Field("", alias='基准单位') |
| 91 | + conversion_factor: None|str |float = Field("", alias='换算系数') |
| 92 | + |
| 93 | + model_config = ConfigDict( |
| 94 | + arbitrary_types_allowed=True, |
| 95 | + populate_by_name=True, |
| 96 | + use_enum_values=True, |
| 97 | + ) |
| 98 | + |
| 99 | +df = pd.read_excel("x.xlsx", na_filter=False, na_values=['NaN']) |
| 100 | +t_list = [] |
| 101 | +for index, row in df.iterrows(): |
| 102 | + t_list.append(UnitInfoRawModel(**row.to_dict())) |
| 103 | +print(t_list) |
| 104 | +``` |
0 commit comments