2

I'm using csvkit to manipulate csv files, but can't find how to do this:

  1. move one column before or after another column
  2. swap two columns but keep others unchanged

Does someone know how to do it?

Freewind
  • 193,756
  • 157
  • 432
  • 708

2 Answers2

3

You could use csvcut

csvcut -c column_c,column_a data.csv > new.csv

Or csvsql

csvsql --table=x --query "select column_c,column_a from x" data.csv

If you are used to sql queries the second might be easier for you.

Yisrael Dov
  • 2,369
  • 1
  • 16
  • 13
  • Thanks but what about other fields? Do I need to list them explicitly even if there are many? – Freewind Oct 26 '15 at 06:13
  • yes you would need to list them explicitly. on csvsql you can use `*` for all rows like in a regular sql query. You might want to check out `awk` – Yisrael Dov Oct 26 '15 at 10:27
1

According to the documentation you linked to, csvcut can do the job. A disadvantage would be that you have to list all of the columns.

An alternative would be to use pandas.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • There are at least six answered questions about swapping columns in a pandas dataframa on StackOverflow alone. I don't think adding one more will make it more clear. – Roland Smith Jul 30 '17 at 07:58