简体   繁体   中英

Sort and Order columns Python

I have a code to create a CSV with information from another CSV file. In my new CSV file, I would like to save only 20 rows sorted from highest to lowest of row ['impressions']

I read something about pandas but I don't find anything about how to do it!

To be more clear, I shared some images:

before: enter image description here

after: enter image description here

Code:

import csv
input_file = 'report_2017_12_11_12_31_19UTC.csv'
output_file= "All_Data_Tags.csv"

with open(input_file) as csvfile, open(output_file,  "w") as output:
    reader = csv.DictReader(csvfile)
    cols = ("domain","ddomain","opportunities", "impressions", "fillRate", "DATA")
    writer = csv.DictWriter(output, fieldnames=cols, extrasaction='ignore')

    writer.writeheader()
    for row in reader:
        row['fillRate'] = '{:.2f}'.format(float(row['fillRate']) * 100)
        if row['ddomain']  == "":
            if row['domain']  == "":
                row['ddomain'] = "App"
                row['domain'] = " "
        if row['domain'] == row['ddomain']:
            row['domain'] = "Real Site"    
        if row['domain']  == "":
            row['domain'] = "Detected Only"
        if row['ddomain']  == "":
            row['ddomain'] = "Vast Media"
        if row['ddomain'] != row['domain']:
            if row['ddomain'] != "Vast Media":
                if row['domain'] != "Real Site":
                    if row['domain'] != "Detected Only":
                        if row['ddomain'] != "App":
                            row['DATA'] = "FAKE"
                        else:
                            row['DATA'] = "OK"
                    else:
                        row['DATA'] = "OK"
                else:
                    row['DATA'] = "OK"
            else:
                row['DATA'] = "OK"

        writer.writerow(row)

Here is the Answer:

code:

import pandas as pd 


movies = pd.read_csv('Top20_Media_Yesterday.csv')

movies = movies.sort_values(['impressions'], ascending=False)

movies = movies.to_csv("Top20_Media_Yesterday.csv")

movies = pd.read_csv('Top20_Media_Yesterday.csv', nrows=21)

movies = movies.to_csv("Top20_Media_Yesterday.csv")

Use the DataFrame.sort_values function of the pandas framework, passing the column name(s),you wish to sort, to the by argument and setting axis to 1.

You can find similar examples here .

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