简体   繁体   中英

How to create new column in excel sheet or csv file in chronological order using python3?

example ==> my excel sheet

column1    column2    column3
 Movie       Thor       Marvel
 actor        xyz        xyz
  xyz         xyz        xyz

now I want to create a new columnx in between column1 and column2 using python

o/p ==> excel or csv sheet

column1   columnx     column2    column3
 Movie     newc1        Thor       Marvel
 actor     newc2          xyz        xyz
  xyz      newc3          xyz        xyz

You canuse pandas to solve this problem as given below,

df = pd.DataFrame({'column1': ["Movie", "actor", "xyz"], 'column2': ["Thor", "xyz", "xyz"],'column3': ["Marvel", "xyz", "xyz"]})

df = pd.read_csv("check.csv")

idx = 1
column_x = ["newc1", "newc2", "newc3"]  
df.insert(loc=idx, column='columnx', value=column_x)
df
   column1 columnx column2 column3
0   Movie   newc1    Thor  Marvel
1   actor   newc2     xyz     xyz
2     xyz   newc3     xyz     xyz

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM