Skip to content

Instantly share code, notes, and snippets.

@bitliner
Last active October 4, 2024 14:07
Show Gist options
  • Save bitliner/217f4162ad4160cd7c969ea73fdc591c to your computer and use it in GitHub Desktop.
Save bitliner/217f4162ad4160cd7c969ea73fdc591c to your computer and use it in GitHub Desktop.

Revisions

  1. bitliner revised this gist Oct 4, 2024. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions pandas.md
    Original file line number Diff line number Diff line change
    @@ -75,9 +75,11 @@ data = {'Name': ['Tom', 'nick', 'krish', 'jack'],
    'Age': [20, 21, 19, 18]}

    df = pd.DataFrame(data)
    ```

    OR

    ```python
    data = [
    {"name": "Alice", "age": 25, "city": "New York"},
    {"name": "Bob", "age": 30, "city": "Los Angeles"},
  2. bitliner revised this gist Oct 4, 2024. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions pandas.md
    Original file line number Diff line number Diff line change
    @@ -75,6 +75,17 @@ data = {'Name': ['Tom', 'nick', 'krish', 'jack'],
    'Age': [20, 21, 19, 18]}

    df = pd.DataFrame(data)

    OR

    data = [
    {"name": "Alice", "age": 25, "city": "New York"},
    {"name": "Bob", "age": 30, "city": "Los Angeles"},
    {"name": "Charlie", "age": 35, "city": "Chicago"}
    ]

    # Create a DataFrame
    df = pd.DataFrame(data)
    ```

    ## filter rows
  3. bitliner revised this gist Dec 2, 2022. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion pandas.md
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,12 @@

    ## merge two dataframe

    Append the columns of df2 to df1

    ```python
    df1.join(df2)
    ```

    There are also way to make sql-join like operation. The one above simply append the columns of df2 to df1

    ## select columns

  4. bitliner revised this gist Dec 2, 2022. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions pandas.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,13 @@
    # pandas

    ## merge two dataframe

    ```python
    df1.join(df2)
    ```

    There are also way to make sql-join like operation. The one above simply append the columns of df2 to df1

    ## select columns

    Selecting columns based on their name
  5. bitliner revised this gist Dec 2, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions pandas.md
    Original file line number Diff line number Diff line change
    @@ -2,15 +2,15 @@

    ## select columns

    **Selecting columns based on their name**
    Selecting columns based on their name

    ```python
    df['hue'] # single column

    df[['alcohol','hue']] # multiple columns
    ```

    **Selecting a subset of columns based on difference of columns**
    Selecting a subset of columns based on difference of columns

    ```python
    df[df.columns.difference([‘alcohol’,’hue’])]
  6. bitliner revised this gist Dec 2, 2022. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions pandas.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,21 @@
    # pandas

    ## select columns

    **Selecting columns based on their name**

    ```python
    df['hue'] # single column

    df[['alcohol','hue']] # multiple columns
    ```

    **Selecting a subset of columns based on difference of columns**

    ```python
    df[df.columns.difference([‘alcohol’,’hue’])]
    ```

    ## rename column

    ```python
  7. bitliner revised this gist Nov 30, 2022. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions pandas.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,11 @@
    # pandas

    ## rename column

    ```python
    df.rename(columns = {'old_col1':'new_col1', 'old_col2':'new_col2'}, inplace = True)
    ```

    ## add column

    ```python
  8. bitliner revised this gist Nov 22, 2022. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions pandas.md
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,12 @@ OR
    df2=df.assign(Discount_Percent=lambda x: x.Fee * x.Discount / 100)
    ```

    OR

    ```python
    ff['Discounted_Price'] = df.apply(lambda row: row.Cost - (row.Cost * 0.1), axis = 1)
    ```

    ## create data frame

    ```python
  9. bitliner revised this gist Nov 17, 2022. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions pandas.md
    Original file line number Diff line number Diff line change
    @@ -46,6 +46,12 @@ df = pd.DataFrame(data)
    df[df.apply(lambda x: x['b'] > x['c'], axis=1)]
    ```

    ## process or transform a column

    ```python
    df['Col4'] = df.apply(lambda row:", ".join([(val if val[0]=='a' else "["+val+"]") for val in row if not pd.isna(val)]), axis=1)
    ```

    ## test dataframe equality

    ```python
  10. bitliner revised this gist Nov 17, 2022. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions pandas.md
    Original file line number Diff line number Diff line change
    @@ -44,4 +44,13 @@ df = pd.DataFrame(data)

    ```python
    df[df.apply(lambda x: x['b'] > x['c'], axis=1)]
    ```

    ## test dataframe equality

    ```python
    from pandas.testing import assert_frame_equal
    df1 = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
    df2 = pd.DataFrame({'a': [1, 2], 'b': [3.0, 4.0]})
    assert_frame_equal(df1, df1)
    ```
  11. bitliner revised this gist Nov 17, 2022. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions pandas.md
    Original file line number Diff line number Diff line change
    @@ -33,16 +33,15 @@ df = pd.DataFrame(data, columns=['Name', 'Age'])

    OR

    ```
    ```python
    data = {'Name': ['Tom', 'nick', 'krish', 'jack'],
    'Age': [20, 21, 19, 18]}

    # Create DataFrame
    df = pd.DataFrame(data)
    ```

    ## filter rows

    ```
    ```python
    df[df.apply(lambda x: x['b'] > x['c'], axis=1)]
    ```
  12. bitliner revised this gist Nov 17, 2022. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions pandas.md
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,16 @@ data = [['tom', 10], ['nick', 15], ['juli', 14]]
    df = pd.DataFrame(data, columns=['Name', 'Age'])
    ```

    OR

    ```
    data = {'Name': ['Tom', 'nick', 'krish', 'jack'],
    'Age': [20, 21, 19, 18]}
    # Create DataFrame
    df = pd.DataFrame(data)
    ```

    ## filter rows

    ```
  13. bitliner revised this gist Nov 17, 2022. 1 changed file with 20 additions and 2 deletions.
    22 changes: 20 additions & 2 deletions pandas.md
    Original file line number Diff line number Diff line change
    @@ -2,17 +2,35 @@

    ## add column

    ```
    ```python
    tutors = ['William', 'Henry', 'Michael', 'John', 'Messi']
    df2 = df.assign(TutorsAssigned=tutors)
    ```

    OR

    ```
    ```python
    df2=df.assign(Discount_Percent=lambda x: x.Fee * x.Discount / 100)
    ```

    ## create data frame

    ```python
    data = [10,20,30,40,50,60]

    # Create the pandas DataFrame with column name is provided explicitly
    df = pd.DataFrame(data, columns=['Numbers'])
    ```

    OR

    ```python
    data = [['tom', 10], ['nick', 15], ['juli', 14]]

    # Create the pandas DataFrame
    df = pd.DataFrame(data, columns=['Name', 'Age'])
    ```

    ## filter rows

    ```
  14. bitliner revised this gist Nov 17, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions pandas.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # pandas

    ### add column
    ## add column

    ```
    tutors = ['William', 'Henry', 'Michael', 'John', 'Messi']
    @@ -13,7 +13,7 @@ OR
    df2=df.assign(Discount_Percent=lambda x: x.Fee * x.Discount / 100)
    ```

    ### filter rows
    ## filter rows

    ```
    df[df.apply(lambda x: x['b'] > x['c'], axis=1)]
  15. bitliner revised this gist Nov 17, 2022. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion pandas.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # pandas

    ## add column
    ### add column

    ```
    tutors = ['William', 'Henry', 'Michael', 'John', 'Messi']
    @@ -11,4 +11,10 @@ OR

    ```
    df2=df.assign(Discount_Percent=lambda x: x.Fee * x.Discount / 100)
    ```

    ### filter rows

    ```
    df[df.apply(lambda x: x['b'] > x['c'], axis=1)]
    ```
  16. bitliner revised this gist Nov 17, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions pandas.md
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,8 @@ tutors = ['William', 'Henry', 'Michael', 'John', 'Messi']
    df2 = df.assign(TutorsAssigned=tutors)
    ```

    OR

    ```
    df2=df.assign(Discount_Percent=lambda x: x.Fee * x.Discount / 100)
    ```
  17. bitliner created this gist Nov 17, 2022.
    12 changes: 12 additions & 0 deletions pandas.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    # pandas

    ## add column

    ```
    tutors = ['William', 'Henry', 'Michael', 'John', 'Messi']
    df2 = df.assign(TutorsAssigned=tutors)
    ```

    ```
    df2=df.assign(Discount_Percent=lambda x: x.Fee * x.Discount / 100)
    ```