Last active
October 4, 2024 14:07
-
-
Save bitliner/217f4162ad4160cd7c969ea73fdc591c to your computer and use it in GitHub Desktop.
Revisions
-
bitliner revised this gist
Oct 4, 2024 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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"}, -
bitliner revised this gist
Oct 4, 2024 . 1 changed file with 11 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
bitliner revised this gist
Dec 2, 2022 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) ``` ## select columns -
bitliner revised this gist
Dec 2, 2022 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
bitliner revised this gist
Dec 2, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,15 +2,15 @@ ## 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’])] -
bitliner revised this gist
Dec 2, 2022 . 1 changed file with 16 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
bitliner revised this gist
Nov 30, 2022 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
bitliner revised this gist
Nov 22, 2022 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
bitliner revised this gist
Nov 17, 2022 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
bitliner revised this gist
Nov 17, 2022 . 1 changed file with 9 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) ``` -
bitliner revised this gist
Nov 17, 2022 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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]} df = pd.DataFrame(data) ``` ## filter rows ```python df[df.apply(lambda x: x['b'] > x['c'], axis=1)] ``` -
bitliner revised this gist
Nov 17, 2022 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ``` -
bitliner revised this gist
Nov 17, 2022 . 1 changed file with 20 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ``` -
bitliner revised this gist
Nov 17, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ # pandas ## 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 ``` df[df.apply(lambda x: x['b'] > x['c'], axis=1)] -
bitliner revised this gist
Nov 17, 2022 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ # pandas ### 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)] ``` -
bitliner revised this gist
Nov 17, 2022 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) ``` -
bitliner created this gist
Nov 17, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) ```