Pandas DataFrame stack multiple column values into single column

borice :

Assuming the following DataFrame:

  key.0 key.1 key.2  topic
1   abc   def   ghi      8
2   xab   xcd   xef      9

How can I combine the values of all the key.* columns into a single column 'key', that's associated with the topic value corresponding to the key.* columns? This is the result I want:

   topic  key
1      8  abc
2      8  def
3      8  ghi
4      9  xab
5      9  xcd
6      9  xef

Note that the number of key.N columns is variable on some external N.

Alexander :

You can melt your dataframe:

>>> keys = [c for c in df if c.startswith('key.')]
>>> pd.melt(df, id_vars='topic', value_vars=keys, value_name='key')

   topic variable  key
0      8    key.0  abc
1      9    key.0  xab
2      8    key.1  def
3      9    key.1  xcd
4      8    key.2  ghi
5      9    key.2  xef

It also gives you the source of the key.


From v0.20, melt is a first class function of the pd.DataFrame class:

>>> df.melt('topic', value_name='key').drop('variable', 1)

   topic  key
0      8  abc
1      9  xab
2      8  def
3      9  xcd
4      8  ghi
5      9  xef

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Coalesce values from 2 columns into a single column in a pandas dataframe

Pandas logical indexing on a single column of a dataframe to assign values

Changing values in multiple columns of a pandas DataFrame using known column values

Filter pandas dataframe rows by multiple column values

Pandas groupby, pivot, or stack? Turn groups of a single column into multiple columns

pandas DataFrame reshape by multiple column values

python pandas dataFrame create single json column of multiple columns value

groupby comma-separated values in single DataFrame column python/pandas

break down pandas dataframe column into multiple columns in a single df

Aggregate values of same name pandas dataframe columns to single column

How to stack column values in a single Dataframe cell

Pandas dataframe, how to set multiple column values in a single row?

replace multiple values in a column and keep other values unchanged in pandas dataframe

How to Split the Contents of a Single Pandas Dataframe Column into Multiple New Columns

Pandas dataframe replace values on multiple column conditions

Changing multiple column values at once for a single row in my dataframe

Stack multiple columns into single column while maintaining other columns in Pandas?

Pandas merge single column dataframe with another dataframe of multiple columns

Condensing Multiple DataFrame Columns into a Single Indicator Column in Pandas

remapping multiple column values with multiple dictionary in dataframe using python pandas

Create multiple column pandas from single column and feed in values

Pandas stack multiple columns to a single column

Adding multiple constant values in a pandas dataframe column

Merge multiple rows in pandas Dataframe based on multiple column values

Pandas: filter one dataframe by multiple, simultaneous column values of another dataframe

pandas single column value to multiple column headers with formatted values

How to print the three rows with the highest values in a single column in a pandas dataframe

Pandas DataFrame stack multiple column values into single column without changing one of the columns

DataFrame: Sort values of a single column