从lis的csv文件组织数据

2024-05-29 03:14:03 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我需要我的代码来获取这个

No Column Sum
0 Company 28
1 Booth 28
2 Full-Time 25
3 Full-Time Visa Sponsor 5
4 Part-Time 1
5 Internship 18
6 Freshman 7
7 Sophomore 9
8 Junior 17
9 Senior 24
10 Post-Bacs 17
11 MS 17
12 PhD 6
13 Alumni 15

但是我的代码现在正在输出这个

Column Sum
Company 27
Booth 27
Full-Time 27
Full-Time Visa Sponsor 27
Part-Time 27
Internship 27
Freshman 27
Sophomore 27
Junior 27
Senior 27
Post-Bacs 27
MS 27
PhD 27
Alumni 27

我不得不使用csv文件中的信息,清理它,现在我必须像这样组织它。我对这部分的代码如下

company_dict = {0:"Company", 1:"Booth",
                2:"Full-Time", 3:"Full-Time Visa Sponsor",
                4:"Part-Time", 5:"Internship",
                6:"Freshman", 7:"Sophomore",
                8:"Junior", 9:"Senior",
                10:"Post-Bacs", 11:"MS",
                12:"PhD", 13:"Alumni"}

                                            #Loop to organize the company_dict
for lines in company_dict:
    print(repr(lines),company_dict[lines])

keywords = ("AIG","Baylor","CGG","Citi","ExxonMobil","Flow-Cal Inc.",                   #I used a list to help me get the information I wanted from the csv file
           "Global SHop Solutions","Harris Count CTS","HCSS",
           "Hitachi Consulting", "HP Inc.","INT Inc.","JPMorgan Chase & Co",
           "Leidos","McKesson","MRE Consulting Ltd.","NetIQ","PROS",
           "San Jacinto College","SAS","Smartbridge","Sogeti USA",
           "Southwest Research Institute","The Reynolds and Reynolds Company",
           "UH Enterprise Systems","U.S. Marine Corps","ValuD Consuting LLC","Wipro")

DataList = []                                                                           #I made a blank list
with f as filterf:                                                                      #This loop will look for the keywords in the file, and only add those keywords
    output_line_counter = 0                                                             #I needed it to print with rows, so I set it to 0
    for line in filterf:
        if any(keyword in line for keyword in keywords):                                #The actual code that looks for keywords in the line in my file
            output_line_counter += 1                                                    #Adds the column (might not be necessary but it works for me)
            DataList.append(line)

CleanerData = sorted(set(DataList))                                                     #I made a new 'cleaner' list so that it would be alphabetically without spaces
line_counter = 0
for i in CleanerData:                                                                   #I had to do another loop to add rows again, it now prints what is required in the question
    line_counter += 1
    print(line_counter, i, end='')

data_employer = {'No': ('Column', 'Sum')}
for empdata in range(14):
    sum = 0
    for i in CleanerData:
        if i[empdata] != '':
            sum += 1
    data_employer[empdata] = (company_dict[empdata], sum)
for k in data_employer:
    print(list(data_employer.keys()).index(k), data_employer[k][0], data_employer[k][1])

我真的不知道27是从哪里来的,我猜这是因为一些逻辑错误,我没有真正看到。这是我在代码和任何输入的尝试将不胜感激。你知道吗

谢谢你!你知道吗

原始CSV文件

ALPHABETICAL ORDER,,,,,,,,,,,,,
,,Positions,,,,Classifications,,,,,,,
Company,Booth,Full-Time,"Full-Time Visa Sponsor",Part-Time,Internship,Freshman,Sophomore,Junior,Senior,Post-Bacs,MS,PhD,Alumni
AIG,10,,,,Yes,,,Jr,,,MS,,
Baylor College of Medicine,19,Yes,Yes,,,,,,,,,,Recent
CGG,17,Yes,Yes,,,,,,,,MS,PhD,Recent
Citi,27/28,Yes,,,Yes,,,Jr,Sr,,,,
ExxonMobil,11,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,,,
,...
Flow-Cal Inc.,16,Yes,,,Yes,,,Jr,Sr,,,,All
Global Shop Solutions,18,Yes,,,Yes,,,,Sr,PB,,,All
Harris County CTS,22,Yes,,,Yes,,,Jr,Sr,PB,MS,PhD,All
HCSS,29,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,Recent
Hitachi Consulting,13,Yes,,,,,,,Sr,,MS,,
HP Inc.,1,Yes,,,Yes,,,Jr,,,MS,,Recent
INT Inc.,20,Yes,Yes,,Yes,,,Jr,Sr,,MS,PhD,
JPMorgan Chase & Co,3,Yes,,,Yes,,,Jr,Sr,,,,
Leidos,390,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,
McKesson,26,Yes,,,,,,,Sr,,,,
,,,,,,,,,,,,,
MRE Consulting Ltd.,2,Yes,,,,,,,Sr,PB,MS,,All
NetIQ,7,,,,Yes,,Soph,Jr,Sr,PB,,,
PROS,21,Yes,,,,,,,Sr,,MS,PhD,All
San Jacinto College  ,14,,,,Yes,,Soph,Jr,Sr,PB,MS,,
SAS,4,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,Recent
Smartbridge,8,Yes,,,,,,,Sr,PB,MS,,
Sogeti USA,15,Yes,,,,,,,Sr,PB,MS,,
Southwest Research Institute,12,Yes,,,Yes,,,Jr,Sr,PB,MS,PhD,All
The Reynolds and Reynolds Company,23,Yes,Yes,,Yes,Fr,Soph,Jr,Sr,PB,,,All
UH Enterprise Systems,9,Yes,Yes,Yes,Yes,Fr,Soph,Jr,Sr,PB,MS,PhD,All
U.S. Marine Corps,25,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,All
ValuD Consuting LLC,5,Yes,,,,,,,Sr,PB,,,All
Wipro,24,Yes,,,,,,,Sr,PB,,,
BOOTH ORDER,,,,,,,,,,,,,
,Booth,Positions,,,,Classifications,,,,,,,
Company,#,Full-Time,"Full-Time
Visa Sponsor",Part-Time,Internship,Freshman,Sophomore,Junior,Senior,Post-Bacs,MS,PhD,Alumni
HP�Inc.,1,Yes,,,Yes,,,Jr,,,MS,,Recent
"MRE Consulting, Ltd.",2,Yes,,,,,,,Sr,PB,MS,,All
JPMorgan Chase & Co,3,Yes,,,Yes,,,Jr,Sr,,,,
SAS,4,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,Recent
ValuD Consuting LLC,5,Yes,,,,,,,Sr,PB,,,All
NetIQ,7,,,,Yes,,Soph,Jr,Sr,PB,,,
Smartbridge,8,Yes,,,,,,,Sr,PB,MS,,
UH Enterprise Systems,9,Yes,Yes,Yes,Yes,Fr,Soph,Jr,Sr,PB,MS,PhD,All
AIG,10,,,,Yes,,,Jr,,,MS,,
ExxonMobil,11,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,,,
Southwest Research Institute,12,Yes,,,Yes,,,Jr,Sr,PB,MS,PhD,All
Hitachi Consulting,13,Yes,,,,,,,Sr,,MS,,
San Jacinto College  ,14,,,,Yes,,Soph,Jr,Sr,PB,MS,,
Sogeti USA,15,Yes,,,,,,,Sr,PB,MS,,
"Flow-Cal, Inc.",16,Yes,,,Yes,,,Jr,Sr,,,,All
CGG,17,Yes,Yes,,,,,,,,MS,PhD,Recent
Global Shop Solutions,18,Yes,,,Yes,,,,Sr,PB,,,All
Baylor College of Medicine,19,Yes,Yes,,,,,,,,,,Recent
"INT, Inc.",20,Yes,Yes,,Yes,,,Jr,Sr,,MS,PhD,
PROS,21,Yes,,,,,,,Sr,,MS,PhD,All
Harris County CTS,22,Yes,,,Yes,,,Jr,Sr,PB,MS,PhD,All
The Reynolds and Reynolds Company,23,Yes,Yes,,Yes,Fr,Soph,Jr,Sr,PB,,,All
Wipro,24,Yes,,,,,,,Sr,PB,,,
U.S. Marine Corps,25,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,All
McKesson,26,Yes,,,,,,,Sr,,,,
Citi,27/28,Yes,,,Yes,,,Jr,Sr,,,,
HCSS,29,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,Recent
Leidos,30,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,

更新:我已经放置了更多的代码来帮助澄清。还在想为什么名单上只印了27张。我不能用熊猫做这个项目


Tags: infortimefrallfullyesms
2条回答

替换最后一行print(data_employer[k][0], data_employer[k][1])

print(list(data_employer.keys()).index(k), data_employer[k][0], data_employer[k][1])

下面是一个使用熊猫的简单解决方案

import pandas as pd

csv_file_in = 'lines.csv'
csv_file_out = 'return.csv'

df = pd.read_csv(csv_file_in, header=2) # Read in CSV header=2 makes the headers ALumni, PhD etc etc

headers = list(df.columns.values) # get a list of columns to count (headers as row 2)

temp_df = pd.DataFrame([]) # create temp df

for i in headers: #iterate through the columns
    try:
        new_df = pd.DataFrame({'Sum': df[i].count().sum()}, index=[i]) # new dataframe as holding (will be overwritten in the next iteration)
        temp_df = pd.concat([new_df, temp_df]) # concat to temp_df
    except KeyError as e:
        print(e)


temp_df.to_csv(csv_file_out) #output to csv
print(temp_df)

输出

                    Sum
Alumni                   15
PhD                       6
MS                       17
Post-Bacs                17
Senior                   24
Junior                   17
Sophomore                 9
Freshman                  7
Internship               18
Part-Time                 1
Full-Time Visa Sponsor    5
Full-Time                25
Booth                    28
Company                  29

相关问题 更多 >

    热门问题