简体   繁体   中英

Python - Linking columns in Excel for sorting

The problem that I have to solve:

I'm trying to automate several processes in excel. I'm currently stuck on the first one. (Also I'm pretty weak at using excel so I apologize in advance if some of the things I saw don't make sense. I scraped data from the internet and inputted into an excel file. I concat'ed that data with a spreadsheet I already had. Here's the code I used to combine files.

import numpy as np
import pandas as pd


def MergeFiles():
    #find both csv files on computer
    baseData = pd.read_csv('pathname') #keep this on the left
    scrapedData = pd.read_csv('pathname') #keep this on the right


    mergedFile = pd.concat([baseData, scrapedData], axis = 1)

    mergedFile.to_csv('pathname', index = False)

MergeFiles()

What I want to do:

Col1 Col2

c 1

b 2

a 3

-Alphabetically Order Col 1 and values in col2 also shift

Col1 Col2

a 3

b 2

c 1

I'm trying to link columns together so if I try to sort all rows go through the same position shift.

Also any help would be appreciated, I tried looking into Pandas documentation and I couldn't find anything related to this problem. I probably missed something so any help would be appreciated!

So apparently the pandas library does all of this automatically through sort_values()

So

scrapedData = scrapedData.sort_values(by = ['colName'], ascending=True,) #sort the scrapedData
    scrapedData.to_csv('pathName', index = False) #replace the file

would do the trick

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