Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert vertical pandas table of 2 columns to horizontal table based on common ID value in python

df1 = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two', 'two'],
                    'bar': ['A', 'B', 'C', 'A', 'B', 'C']})
foo bar
0 one A
1 one B
2 one C
3 two A
4 two B
5 two C

I would like to convert this to

foo val1 val2 val3
One A B C
Two A B C

And the code I tried is:

pd.pivot_table(df1,index='foo',aggfunc=['first'])

But the above code is returning only the first value

like image 472
vinay kumar Avatar asked Jul 26 '21 20:07

vinay kumar


People also ask

How do I change the orientation of a pandas DataFrame?

Use the T attribute or the transpose() method to swap (= transpose) the rows and columns of pandas. DataFrame . Neither method changes the original object but returns a new object with the rows and columns swapped (= transposed object).

How do you convert rows into columns and columns into rows in pandas?

Pandas DataFrame: transpose() function The transpose() function is used to transpose index and columns. Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. If True, the underlying data is copied. Otherwise (default), no copy is made if possible.

How do you convert a DataFrame to a 2d list in Python?

By using the Concept of to_numpy(). tolist method we can easily convert Pandas DataFrame into a list of 2d lists, by converting either each row or column. To do this first we have to create a list of tuples and then create a dataframe object 'new_val'.

How do I convert columns to rows in pandas?

Method #2: Using pivot() method. In order to convert a column to row name/index in dataframe, Pandas has a built-in function Pivot. Now, let's say we want Result to be the rows/index, and columns be name in our dataframe, to achieve this pandas has provided a method called Pivot.

How to convert two columns into a dictionary in pandas Dataframe?

Another approach to convert two column values into a dictionary is to first set the column values we need as keys to be index for the dataframe and then use Pandas’ to_dict () function to convert it a dictionary. This creates a dictionary for all columns in the dataframe. Therefore, we select the column we need from the “big” dictionary.

How to stack 2 pandas series horizontally in Python?

Stacking Horizontally : We can stack 2 Pandas series horizontally by passing them in the pandas.concat () with the parameter axis = 1. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

How to create a frequency table of column in pandas for state?

Frequency table of column in pandas for State column can be created using crosstab () function as shown below. crosstab () function takes up the column name as argument counts the frequency of occurrence of its values 1 ### frequency table using crosstab ()function 2

How to transpose pandas Dataframe in Python?

You can use the following syntax to transpose Pandas DataFrame: Let’s see how to apply the above syntax by reviewing 3 cases of: To start with a simple example, let’s create a DataFrame with 3 columns: Run the code in Python, and you’ll get the following DataFrame (with a default numeric index that starts from 0 as highlighted in yellow):


Video Answer


3 Answers

We can enumerate groups with groupby cumcount and use those as the pivot columns then add_prefix to the numerical values and reset_index to return the 'foo' values to the columns:

new_df = (
    df1.pivot_table(index='foo',
                    columns=df1.groupby('foo').cumcount() + 1,
                    values='bar', 
                    aggfunc='first')
        .add_prefix('val')
        .reset_index()
)
   foo val1 val2 val3
0  one    A    B    C
1  two    A    B    C

See how df1.groupby('foo').cumcount() + 1 makes the columns:

   foo  columns
0  one        1  # First instance of "one"
1  one        2  # Second instance of "one"
2  one        3  # Third instance of "one"
3  two        1
4  two        2
5  two        3

Code to generate the above DataFrame:

demo_df = pd.DataFrame({
    'foo': df1['foo'],
    'columns': df1.groupby('foo').cumcount() + 1
})
like image 76
Henry Ecker Avatar answered Oct 20 '22 18:10

Henry Ecker


Try:

df1.groupby([df1.groupby('foo').cumcount() + 1,
             'foo']).first()['bar'].unstack(0).add_prefix('val').reset_index()

Output:

   foo val1 val2 val3
0  one    A    B    C
1  two    A    B    C
like image 28
Scott Boston Avatar answered Oct 20 '22 20:10

Scott Boston


Another solution:

x = df1.pivot("foo", "bar", "bar")
x.columns = [f"var{i}" for i in range(1, len(x.columns) + 1)]
x = x.reset_index()
print(x)

Prints:

   foo var1 var2 var3
0  one    A    B    C
1  two    A    B    C
like image 39
Andrej Kesely Avatar answered Oct 20 '22 19:10

Andrej Kesely