Global AI and Data Science

 View Only
  • 1.  Data Manipulation Python

    Posted Mon November 09, 2020 11:48 AM
    Hi community
    please How could I group the attached table as shown in the picture?
    thank you


    ------------------------------
    abdellah sadk
    ------------------------------

    #GlobalAIandDataScience
    #GlobalDataScience


  • 2.  RE: Data Manipulation Python

    Posted Tue November 10, 2020 05:44 AM
    It sounds like a case for Pandas.

    ------------------------------
    Matthias Jungbauer
    ------------------------------



  • 3.  RE: Data Manipulation Python

    Posted Tue November 10, 2020 11:47 AM
    Hello Abdellah,

    I guess that this function `pivot_table` would help you.

    check this example:
    https://datascience.stackexchange.com/questions/33879/panda-grouping-by-month-with-transpose

    Best regards,

    ------------------------------
    Franco Yair Benko
    ------------------------------



  • 4.  RE: Data Manipulation Python

    Posted Wed November 11, 2020 03:21 AM
    Edited by System Fri January 20, 2023 04:09 PM
    Hi Abdellah
    Kindly find the code snippet below for the required conversion:

    import pandas as pd
    # creating the sample dataset
    raw_data = {'grade': ['A','A','A','B','B','B','B','C','C','C'],
    'Num_Student': [3,6,5,8,8,6,4,5,6,7]}
    df = pd.DataFrame(raw_data, columns = ['grade', 'Num_Student'])
    #view the dataframe
    print(df)
    # Grouping the duplicate 'grades' and creating a list of 'Num_Student'
    df = df['Num_Student'].groupby([df.grade]).apply(list).reset_index()
    # expand df.Num_Student into its own dataframe
    tags = df['Num_Student'].apply(pd.Series)
    # rename each variable
    tags = tags.rename(columns = lambda x : 'Num_Student_' + str(x))
    # join the tags dataframe back to the original dataframe
    df = pd.concat([df[:], tags[:]], axis=1)
    # drop the 'Num_Student' columnn
    df = df.drop('Num_Student',axis=1)
    print(df)


    Let me know if it does not work.


    ------------------------------
    Ayush Jain
    ------------------------------



  • 5.  RE: Data Manipulation Python

    Posted Thu November 12, 2020 11:30 AM
    Hi,

    The below code snippet should work you .

    import pandas as pd

    df = pd.DataFrame([['A',3],['A',6],['A',5],['B',8],['B',8],['B',6],['B',4],['C',5],['C',6],['C',7]],columns=['col1','col2'])
    new_df = df.set_index('col1').groupby(['col1'])['col2'].apply(list).reset_index('col1')
    final_df = pd.concat([new_df['col1'],pd.DataFrame(new_df['col2'].values.tolist())], axis=1)

    ------------------------------
    Kaustuv Chakraborti
    ------------------------------



  • 6.  RE: Data Manipulation Python

    Posted Tue May 11, 2021 10:49 AM
    Hi Abdellah
    I would highly recommend you to solve this by using pandas and reading the official documentation.
    I think the section of Reshaping and pivot table:

    https://pandas.pydata.org/docs/user_guide/reshaping.html

    can be particularly useful to solve your problem. 




    ------------------------------
    Fernando Resendiz
    ------------------------------