-3

I need to read a csv file in python and then re arrange the columns of csv and make a new dataframe made of the rearranged columns

I tried using list, but it might work slow..
Any alternative using numpy or pandas?

Edit: I am rearranging the row using df.reindex()

I am currently doing this and thus exporting the df after leaving 4 rows blank

df_reindexed.to_excel(writer, sheet_name='Sheet1',startrow=4, index=False)

I need to add format and text to cells in those top 4 rows, corresponding to the column name in the following rows. I know I can use iloc, but is there anyway to do it so that i can select a cell above a cell with specified name?

Mouse on the Keys
  • 322
  • 1
  • 5
  • 13
MG123
  • 1
  • 1
  • 2
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Jul 03 '18 at 10:48
  • Depends. Numpy wins in speed if done correctly but pandas is convenient in programming speed (pandas does a lot of nice guesses). Show us some data example as jezrael points out! (And something you tried) – Anton vBR Jul 03 '18 at 10:56

2 Answers2

0
import pandas as pd
# read a CSV with pandas 
src = "your/path"
old_df = pd.read_csv(src, sep=",")

# the columns that you want
desired_cols = ['c1','c2']

# pandas will return a new df only with the columns that you want
new_df = old_df[desired_cols]
alvaro nortes
  • 570
  • 4
  • 10
0

Another way to do it is:

desired_cols = ['c1', 'c2', 'c3']
df_final = df_final.reindex(columns = desired_cols)
Ankur Sinha
  • 6,473
  • 7
  • 42
  • 73