2
convert columns into rows according to the value of the row using pandas
source link: https://www.codesd.com/item/convert-columns-into-rows-according-to-the-value-of-the-row-using-pandas.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
convert columns into rows according to the value of the row using pandas
advertisements
I have an excel file as follows:
ID wk48 wk49 wk50 wk51 wk52 wk1 wk2
1123 10 22 233 2 4 22 11
1198 9 4 44 23 34 5 234
101 3 6 3 43 33 34 78
I want the output as follows in python
1123 wk48 10
1123 wk49 22
1123 wk50 233
1123 wk51 2
1123 wk52 4
1123 wk1 22
1123 wk2 11
1198 wk48 9
1198 wk49 4
1198 wk50 44
1198 wk51 23
1198 wk52 34
1198 wk1 5
1198 wk2 234
Any suggestions
The first thing I would do is set ID
as your index with:
df.set_index('ID',inplace=True)
Next, you can use the following command to reorient your dataframe:
df = df.stack().reset_index()
print df
-------------------------------------------
Output:
ID level_1 0
0 1123 wk48 10
1 1123 wk49 22
2 1123 wk50 233
3 1123 wk51 2
4 1123 wk52 4
5 1123 wk1 22
6 1123 wk2 11
7 1198 wk48 9
8 1198 wk49 4
9 1198 wk50 44
10 1198 wk51 23
11 1198 wk52 34
12 1198 wk1 5
13 1198 wk2 234
14 101 wk48 3
15 101 wk49 6
16 101 wk50 3
17 101 wk51 43
18 101 wk52 33
19 101 wk1 34
20 101 wk2 78
Related Articles
Unix awk scripting to convert columns into rows
Need help to convert rows to columns in unix scripting. My source is file system. Tried the below script: `perl -nle ' if($. == 1) { (@a)=/([\w - .]+)(?=,|\s*$)/g } else { (@b)=/([\w - .]+)(?=,|\s*$)/g; print "$a[0]|$b[0]|$b[1]|$b[2}|$a[$_]|$b[$_+3]&
JavaScript code or framework for converting columns into rows and rows into columns in an HTML table or div grids
I am looking for a script or JavaScript framework plugin which can help me to manipulate HTML grids. On some event call, columns and rows should be interchanged.That's a simple function, assuming that there are exactly n*m cells (no colspans/rowspans
How to convert columns into rows in Oracle?
I have a table in this form (this is just the partial view, the table contains more than 100 columns). LOAN NUMBER DOCUMENT_TYPE DOCUMENT_ID 992452533663 Voters ID XPD0355636 992452533663 Pan card CHXPS5522D 992452533663 Drivers licence DL-0420110141
PostgreSQL converts columns into rows? Transpose?
I have a PostgreSQL function (or table) which gives me the following output: Sl.no username Designation salary etc.. 1 A XYZ 10000 ... 2 B RTS 50000 ... 3 C QWE 20000 ... 4 D HGD 34343 ... Now I want the Output as below: Sl.no 1 2 3 4 ... Username A
How to convert columns into rows in SQL Server
Please consider this table: ID Page Line C01 C02 C03 --------------------------------------------------------------------- 1 122 11 1 0 1 1 123 11 1 1 1 1 124 12 0 0 0 1 125 16 1 0 1 1 127 11 0 1 0 I want to convert this table to this one: ID Page Li
How to divide columns into rows using SSRS
I have a tablix that has a lot of columns, but just one row. I wanna to split these columns in different rows, just for visualization. What I have: Column 1 | Column 2 | Column 3 | Column 4 | Column 5 | Column 6 What I want: Column 1 | Column 2 Colum
SQL query to convert columns to rows
I have a table as shown below.My question is:How can I convert columns into rows? I'm using Microsoft SQL Server sip_RECno user1 user2 user3 user4 1 ram ravi sam raj i need op like below user ram ravi sam raj how to do it? thanksYour Data DECLARE @TA
How do I convert columns into a single pipe-separated column without losing the scientific notation?
I have used this VBA code to convert columns into pipe separated format. However, the data contains numbers in scientific notation (eg 2.000000e-01) which Excel automatically converts into 0.2 which I don't want. I have tried changing this code: var
How do I transform Sql columns into rows?
I have a very simple problem which requires a very quick and simple solution in SQL Server 2005. I have a table with x Columns. I want to be able to select one row from the table and then transform the columns into rows. TableA Column1, Column2, Colu
Convert columns to rows
My general problem is that I have 10000 distinct numbers as a list of (i.e. 13, 22, 443, ..., 5010 - not in a table AND CANNOT create a table of them) and i need to loop through these numbers in chunks (1..100, 101..200, 202..300, etc. - 1st 100 numb
Convert columns to rows and column names in SQL Server
I want to convert columns to rows in SQL Server: Id Value Jan1 Jan2 ---------------------- 1 2 25 35 2 5 45 45 result should be Id Value Month 1 2 ---------------------- 1 2 Jan 25 35 2 5 Jan 45 45 How can I get this result? Anyone please helpWhat yo
How to hide a column in gridview accordingly in the header field?
i am using a gridview to bind my datas. i want to hide a column from backend accordingly to the headerfield <asp:TemplateField> <HeaderTemplate> Incident Number </HeaderTemplate> <ItemTemplate> <asp:Label ID="lb_incidentnum
Divide the file into several according to the contents of the variable
I want to split file according to it's content. My dummy file looks like that: info info info cat info info info cow info info info dog info info info dinosaur info info info bat $4 words starts with different letters (C,D,B) - I want to split file i
Transform the column into rows in the SQL Server table
I have a query in which I want some columns to be appear as rows. The query is Select * From Emp_mon_day Where emp_mkey IN (select emp_card_no from emp_mst where comp_mkey in (7, 110)) and Year = 2016 and month = 2 and Emp_mkey = 2492 with this outpu
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK