Summary and Cheatsheet of Command for csv File - Python Automation and Machine Learning for ICs - - An Online Book - |
|
http://www.globalsino.com/ICs/ |
================================================================================= Table 4712a. Data selection by label (with df.loc).
|
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, :] |
|
|||||||||||||||||||
df.loc[df.index > 100, :] |
|
|||||||||||||||||||
df.loc[df["User"].isin(["xyz", "BMO"]), :] |
|
Table 4712c. Data selection when DataFrames consist of only numbers.
Dataframe (df) | Python code: df < 60 | Python code: df[df < 60] | |||||||||||||||||||||||||||
|
|
|
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.
================================================================================= |