How to reorder the columns of a CSV?

2024/11/13 9:38:47

How can I re-order the columns of a CSV file using Python? These are the first rows of a CSV file I need to change:

03;30269714;Ramiro Alberto;Nederz;active;pgc_gral
03;36185520;Andrea;Espare;active;pgc_gral
03;24884344;Maria Roxana;Nietto;active;pgc_gral
03;27461021;Veronica Andrea;Lantier;active;pgc_gral
71;24489743;Diego;Moneta;active;pgc_gral

This is the desired output:

30269714;pgc_gral; Ramiro Alberto;Nederz
36185520;pgc_gral; Andrea;Espare
24884344;pgc_gral;Maria Roxana;Nietto
27461021;pgc_gral;Veronica Andrea;Lantier
24489743;pgc_gral;Diego;Moneta

Column 2 is now column 1, column 6 is column 2, columns 3 and 4 should stay the same and column 5 should be discarded.

Answer

Try this:

import csvwith open('yourfile.csv') as f:reader = csv.reader(f, delimiter=';')for row in reader:print(";".join([row[1], row[5], row[2], row[3]]))
https://en.xdnf.cn/q/119725.html

Related Q&A

Distance matrix in Python Pandas

I am a newbie in python, but I like to process data in pandas. I have a hundred pairs of CSV data such as passenger and bus stop data. The passenger structure data is Person, and XY coordinates (UTM-Me…

calculating catalan numbers using memoization

I am tring to use memoization in order to calculate catalan numbers, but it just does not seem to work, what do I need to change?def catalan_mem(n, memo = None):if n==0:return 1if memo == None:memo = …

cron python file doesnt work on centos 7

im trying to schedule my pythohn script into Centos 7 with cron. On my script at start i have added this:#!/usr/local/bin/pythonand this is my cron file that i have create into folder that contain pyth…

Comma separated Matrix from txt files - continued

I need to form a matrix from a list of textfiles containing frequency distribution of expressions. Therefore, I created a list of all that text files (lof) from a directory and used it to build a matri…

Questions about training LLMs on large text datasets for text generation from scratch

I made a fully custom made GPT in Jax (with Keras 3), using Tensorflow for the data pipeline. Ive trained the model on the Shakespeare dataset and got good results (so no problem with the model). Now I…

Pandas - Update/Merge 2 Dataframes based on multiple matching column values

I have 2 dataframes left_df and right-df, which both have 20 columns with identical names and dtypes. right_df also has 2 additional columns with unique values on every row. I want to update rows in ri…

How do I fix scrapy Unsupported URL scheme error?

I collect url from command python and then insert it into start_urls from flask import Flask, jsonify, request import scrapy import subprocessclass ClassSpider(scrapy.Spider):name = mySpider#sta…

comparing two timeseries dataframes based on some conditions in pandas

I have two timeseries dataframes df1 and df2: df1 = pd.DataFrame({date_1:[10/11/2017 0:00,10/11/2017 03:00,10/11/2017 06:00,10/11/2017 09:00],value_1:[5000,1500,np.nan,2000]})df1[date_1] = pd.to_dateti…

Game of Chance in Python 3.x?

I have this problem in my python code which is a coinflip game, the problem is that when It asks, "Heads or Tails?" and I just say 1 or Heads(same for 2 and Tails) without quotation marks an…

Count occurence of a word by ID in python

Following is the content of a file,My question is how to count the number of occurences for the word "optimus" for different IDs ID67 DATEUID Thank you for choosing Optimus prime. Please w…