PythonML
Summary and Cheatsheet of Command for csv File
- Python Automation and Machine Learning for ICs -
- An Online Book -
Chapter/Index: Introduction | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | Appendix
http://www.globalsino.com/ICs/  


=================================================================================

Table 4712a.  Data selection by label (with df.loc).

 Selection Return Data Type   Example Details  Output 
 Single value Scalar df.loc[100, "User"]  Using scalars for both row and column selection returns a scalar  One cell value 
 One column (1d) Series  df.loc[:, "User"]  Shortcut: df.loc["User"]  
 One column (1d) Series  df.loc[[101, 102], "User"]  Using a scalar on either the row or column selection returns a Series  Two cell values 
 One column (2d) DataFrame  df.loc[:, ["User"]]     
Multiple rows and columns DataFrame  df.loc[:, ["User", "age"]]  Selecting multiple rows and columns returns a DataFrame  Multiple rows and two columns 
 Range of columns DataFrame  df.loc[:, "name":"User"]     
 One row (1d) Series  df.loc[100, :]     
 One row (2d) DataFrame  df.loc[[100], :]     
 Multiple rows DataFrame  df.loc[[103, 100], :]     
 Range of rows DataFrame  df.loc[100:102, :]     

Table 4712b.  Data selection by position.

 Selection Return Data Type   Example Details  Output 
 Single value Scalar df.iloc[1, 2]  Returns a Scalar  Single cell value 
 One column (1d) Series  df.iloc[:, 2]     
 One column (2d) DataFrame  df.iloc[:, [2]]     
Multiple rows and columns DataFrame  df.iloc[:, [2, 1]]  Returns a DataFrame  Multiple rows and two columns  
 Multiple rows Series   df.iloc[[0, 2], 1]  Returns a Series  Two cell values 
 Range of columns DataFrame  df.iloc[:, :3]     
 One row (1d) Series  df.iloc[1, :]     
 One row (2d) DataFrame  df.iloc[[1], :]     
 Multiple rows DataFrame  df.iloc[[3, 1], :]     
 Range of rows DataFrame  df.iloc[1:3, :]     

 Table 4712c. Data selection by boolean indexing. Boolean operators: and (&), or (|) and not (~).

 Example Details   Output
db = (df["age"] > 50) & (df["User"] == "Kevin") Return a series with only True/False    A list of True and False
df.loc[db, :]   
 User ID  Name  age  User  Grade  Country
 101  Kevin  51  xyz  87  USA
 df.loc[df.index > 100, :]  
 User ID  Name  age  User  Grade  Country
 101  Kevin  51  xyz  87  USA
 102  Mike  13  MON  99  USA
df.loc[df["User"].isin(["xyz", "BMO"]), :]  
 User ID  Name  age  User  Grade  Country
 101  Kevin  51  xyz  87  USA
 103  Dan  20  BMO  99  USA

 Table 4712c. Data selection when DataFrames consist of only numbers.

 Dataframe (df)  Python code: df < 60   Python code: df[df < 60]
 Age 1  Age 2  Age 3
 20  50  100
 40  88  90
 Age 1  Age 2  Age 3
 True  True False
 True  False  False
 Age 1  Age 2  Age 3
 20  50  NaN
 40  NaN  NaN

 Table 4712d. Data joining and merging. 

 Type  Description  Script Example   Details
inner  Only rows whose (row) index exists in both DataFrames  df1.join(df2, how="inner")    Indices overlap 
 left All rows from the left DataFrame, matching rows from the right DataFrame  df1.join(df2, how="left")    Indices overlap
 right All rows from the right DataFrame, matching rows from the left DataFrame  df1.join(df2, how="right")    Indices overlap
 outer The union of row indices from both DataFrames  df1.join(df2, how="outer")    Indices overlap
inner  Only rows whose (row) index exists in both DataFrames   df1.merge(df2, how="inner", on=["user"])  df1["user"] = ["a", "b", "c"] df2["user"] = ["c", "b"] Join on one or more DF columns instead of relying on the index. merge accepts the on argument to provide one or more columns as the join condition.
Output: df["user"] = ["c", "b"]
left  All rows from the left DataFrame, matching rows from the right DataFrame df = df1.merge(df2, how="left", on=["user"])

 Table 4712e. CSV structure.

 Structure  Details
 Header next() for csv.reader, df.columns   
 

 Table 4712f. Functions for CSV files.

 Function  Details
 Save Save images  

 Table 4712g. Other applications.

 Function  Details
Merge two csv files    
Rename the duplicated header names in a CSV file
Change/rename a column name/header in a CSV file  
Plot images from dataframe from CSVs  
Read specific cells (cell by cell) in csv file   
Write row-by-row into csv file  

 

 

 

       

        

=================================================================================