# pandas ## merge two dataframe Append the columns of df2 to df1 ```python df1.join(df2) ``` ## 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 df.rename(columns = {'old_col1':'new_col1', 'old_col2':'new_col2'}, inplace = True) ``` ## 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) ``` OR ```python ff['Discounted_Price'] = df.apply(lambda row: row.Cost - (row.Cost * 0.1), axis = 1) ``` ## 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']) ``` OR ```python 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"}, {"name": "Charlie", "age": 35, "city": "Chicago"} ] # Create a DataFrame df = pd.DataFrame(data) ``` ## filter rows ```python 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 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) ```