Python Forum
How to transform from wide to long format in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to transform from wide to long format in python
#2
I think you are overthinking/overcomplicating this problem.

Is it save to state that
  • You want first 8 columns (i.e. A-H), then last 2 columns, then the 9th and 10th columns
  • The column names for first 10 columns are the same as in the original file, the last 2 columns (former 9th and 10th) will have new header - AttributeName and AttributeValue

fname = 'VY6_Row1_report_url_detections_per_frame.csv'
# fname = 'VY6_Row2_report_url_detections_per_frame.csv'

df =  pd.read_csv(fname)
col_drop = len(df.columns) - 10
df['AttributeName'] = df[df.columns[8]]
df['AttributeValue'] = df[df.columns[9]]
df.drop(labels=df.columns[8:8+col_drop], axis=1, inplace=True)
df.to_csv('VY6_out.csv', index=False)
Do you need to use pandas? Using csv module would also do.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: How to transform from wide to long format in python - by buran - Nov-21-2021, 11:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Looking for documentation on Reportlab's canvas.transform() function NeilUK 1 652 Aug-23-2023, 01:21 PM
Last Post: NeilUK
  Transform Dic to dataframe d9d9d 4 1,430 Apr-14-2022, 09:35 AM
Last Post: perfringo
  Transform 3 Columns into Single Column DaveG 8 1,936 Apr-04-2022, 08:42 AM
Last Post: Pedroski55
  Long-term stable source to get news headlines with Python? sandufi 4 1,990 Dec-23-2021, 09:48 AM
Last Post: sandufi
  Importing Program Wide JarredAwesome 4 2,237 Sep-07-2020, 04:34 PM
Last Post: JarredAwesome
  Python long running script - causes RDP failure to server? william101 1 2,374 Jun-08-2020, 08:18 AM
Last Post: nuffink
  What is the best way to set application-wide config values? ajorona 1 1,936 May-07-2020, 05:03 PM
Last Post: buran
  Transform list or set regardless of nesting structure blubb 2 1,992 Mar-10-2020, 07:17 PM
Last Post: ibreeden
  output a list of random numbers 'x' columns wide adityavpratap 4 3,077 Jan-13-2020, 05:32 PM
Last Post: perfringo
  Long command with characters not working in Python on Oracle Linux 7 iaas_infra 10 6,315 Jul-19-2019, 04:53 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020