Python Statistics on Social Science Project Counts

Author: Li Shengsheng (A Little Ant in Management)

Email:[email protected]

Data Source: National Office for Philosophy and Social Science Work

Using Python coding for statistics, due to some units not being included in the following codes, the counts for youth projects and general projects may be slightly underestimated. I have not verified this, so I hope everyone can modify the code as needed:

invalid_units = {'责任单位', '略', '题目略', '', ' ', '无'}
valid_suffixes = (
    '大学', '学院', '研究所', '研究院', '社科院', '党校',
    '出版社', '学会', '科学院', '中心', '学校', '博物馆', '团校',
    '杂志社', '针灸所', '协会', '联合会', '档案馆', '管理处',
    '图书馆', '博物院', '文保所', '考古所', '文研所',
    '医院', '剧院', '体育馆', '观测站', '实验站', '疗养院',
    '指挥部', '办公室', '委员会', '基金会', '交易所', '工作站'
)

If you are interested in the code and results, reply with the keywordSocial Science.

Python Statistics on Social Science Project Counts

Top 100 Project Counts

7

0

0

Rank Unit Name Youth Projects General Projects Key Projects Western Projects Total Project Count
1 Sichuan University 21 22 5 16 64
2 Sun Yat-sen University 28 31 2 0 61
3 Peking University 36 16 7 0 59
4 Xiamen University 21 24 8 0 53
5 Shandong University 25 20 6 0 51
6 Wuhan University 18 26 6 0 50
6 Nanjing University 26 20 4 0 50
6 Anhui University 26 21 3 0 50
6 Renmin University of China 23 22 5 0 50
10 Shanghai Jiao Tong University 26 21 2 0 49
11 Southwest University 17 16 3 11 47
11 Fudan University 16 25 6 0 47
11 Zhejiang University 24 19 4 0 47
14 Jinan University 17 24 5 0 46
15 Central China Normal University 21 17 5 0 43
15 Zhengzhou University 19 22 2 0 43
15 Nanjing Normal University 21 20 2 0 43
18 Yunnan University 14 16 3 9 42
18 Nankai University 13 23 6 0 42
20 Xi’an Jiaotong University 13 15 8 5 41
20 Hunan Normal University 14 21 6 0 41
20 Jilin University 21 15 5 0 41
23 Central South University of Finance and Economics 12 25 2 0 39
23 Hunan University 16 16 7 0 39
25 Beijing Normal University 14 18 6 0 38
25 Fujian Normal University 13 22 3 0 38
27 East China Normal University 9 23 5 0 37
28 Minzu University of China 16 15 5 0 36
29 Henan University 17 15 3 0 35
29 China University of Political Science and Law 19 14 2 0 35
31 Southeast University 21 11 2 0 34
32 Guangzhou University 13 18 2 0 33
33 Tsinghua University 18 6 8 0 32
33 Chongqing University 10 11 3 8 32
35 Capital University of Economics and Business 21 5 5 0 31
35 Shanghai Normal University 11 15 5 0 31
37 Shanxi University 9 19 2 0 30
37 Central South University 18 7 5 0 30
39 Zhejiang Gongshang University 9 15 5 0 29
39 South China Normal University 9 16 4 0 29
41 Guangdong University of Foreign Studies 11 14 3 0 28
41 Northeast Normal University 8 20 0 0 28
43 Huazhong University of Science and Technology 7 18 2 0 27
43 Soochow University 10 13 4 0 27
43 Guizhou University 9 7 1 10 27
43 Hubei University 16 9 2 0 27
43 Lanzhou University 9 7 3 8 27
43 Tongji University 13 10 4 0 27
43 Northwest University 8 11 5 3 27
50 Huaqiao University 6 19 1 0 26
50 Tianjin Normal University 16 9 1 0 26
50 Ningbo University 7 17 2 0 26
50 Jiangxi Normal University 7 15 4 0 26
50 Shanghai University 6 18 2 0 26
50 Shanxi Normal University 9 6 4 26
56 Xiangtan University 9 16 0 0 25
56 Ningxia University 7 8 1 9 25
56 Shandong Normal University 8 14 3 0 25
59 Jiangxi University of Finance and Economics 6 16 2 0 24
59 East China University of Political Science and Law 10 12 2 0 24
59 Nanchang University 8 13 3 0 24
59 Inner Mongolia University 7 9 2 6 24
59 Shandong University of Finance and Economics 9 13 2 0 24
59 Guizhou Normal University 5 1 2 16 24
59 Zhejiang University of Finance and Economics 12 10 2 0 24
59 Southwest University of Political Science and Law 7 9 2 6 24
59 Guangxi Normal University 8 9 1 6 24
59 Chongqing Technology and Business University 2 12 0 10 24
69 Shandong University of Technology 11 10 2 0 23
69 Henan Normal University 6 14 3 0 23
69 Shenzhen University 8 12 3 0 23
69 Yunnan University of Finance and Economics 4 11 1 7 23
69 Zhejiang Normal University 9 10 4 23
74 South China University of Technology 14 8 0 0 22
74 Xinjiang University 6 4 0 12 22
74 Nanjing University of Finance and Economics 8 11 3 0 22
77 Guangxi University 6 7 4 4 21
77 Central Party School of the Communist Party of China (National Academy of Administration) 16 4 1 0 21
77 Beijing Technology and Business University 7 13 1 0 21
77 Shanghai University of International Business and Economics 6 12 3 0 21
77 Yunnan Normal University 6 2 0 13 21
82 Anhui Normal University 9 9 2 0 20
82 Tianjin University of Finance and Economics 10 10 0 0 20
82 Nanjing University of Posts and Telecommunications 12 8 0 0 20
85 Inner Mongolia Normal University 3 4 2 10 19
85 Dalian University of Technology 8 8 3 0 19
85 Sichuan Normal University 4 7 0 8 19
85 Hebei University 3 15 1 19
85 Changzhou University 10 8 1 0 19
85 Central University of Finance and Economics 5 14 0 0 19
85 Hunan University of Technology and Business 6 12 1 0 19
85 Southwest University of Finance and Economics 5 9 2 3 19
85 Hangzhou Dianzi University 9 8 2 0 19
85 Chongqing Normal University 3 4 1 11 19
85 Party School of the Zhejiang Provincial Committee of the Communist Party of China 17 1 1 0 19
85 Zhejiang University of Technology 7 11 1 0 19

0

Rank Unit Name Youth Projects General Projects Key Projects Western Projects Total Project Count
97 Beijing Foreign Studies University 12 5 1 0 18
97 Capital Normal University 6 11 1 0 18
97 Fuzhou University 9 7 2 0 18
97 Ocean University of China 9 9 0 0 18
97 Shanghai Academy of Social Sciences 11 7 0 0 18
97 Qingdao University 7 10 1 0 18
97 Shanxi University of Finance and Economics 5 12 1 0 18
97 Nanjing University of Information Science and Technology 6 11 1 0 18
97 Hainan University 2 8 2 6 18
97 Qufu Normal University 1 16 1 0 18
97 Northwest Minzu University 4 4 3 7 18
108 Nanjing Audit University 9 7 1 0 17
108 Hohai University 5 12 0 0 17
108 Guizhou University of Finance and Economics 7 6 0 3 16
108 Shandong University of Technology and Business 5 11 0 0 16
108 University of International Business and Economics 7 8 1 16

Python Code

import os
import pdfplumber
import pandas as pd
from collections import Counter, OrderedDict, defaultdict
import re

# ---------- Configuration Area ----------
data_folder = r'C:\Users\lisheng\Desktop\data'   # PDF location & output directory

file_map = OrderedDict([
    ('Youth Projects', 'Youth Projects.pdf'),
    ('General Projects', 'General Projects.pdf'),
    ('Key Projects', 'Key Projects.pdf'),
    ('Western Projects', 'Western Projects.pdf')
])

# Define valid unit suffixes and invalid unit names
invalid_units = {'责任单位', '略', '题目略', '', ' ', '无'}
valid_suffixes = (
    '大学', '学院', '研究所', '研究院', '社科院', '党校',
    '出版社', '学会', '科学院', '中心', '学校', '博物馆', '团校',
    '杂志社', '针灸所', '协会', '联合会', '档案馆', '管理处',
    '图书馆', '博物院', '文保所', '考古所', '文研所',
    '医院', '剧院', '体育馆', '观测站', '实验站', '疗养院',
    '指挥部', '办公室', '委员会', '基金会', '交易所', '工作站'
)
# -------------------------------------

def is_valid_unit(unit_name):
    """Check if the unit name is valid"""
    if not unit_name or unit_name in invalid_units:
        return False
    
    # Use regex to check for valid suffixes
    pattern = '|'.join(valid_suffixes)
    return bool(re.search(pattern, unit_name))

def extract_units(pdf_path):
    """Extract all unit names from the PDF, handling line breaks"""
    units = []
    with pdfplumber.open(pdf_path) as pdf:
        for page in pdf.pages:
            # Extract table data
            tables = page.extract_tables()
            if not tables:
                continue
                
            for table in tables:
                for row in table:
                    # Ensure the row has enough columns
                    if len(row) > 4 and row[4]:
                        unit = row[4].strip().replace('\n', '').replace('\r', '')
                        if is_valid_unit(unit):
                            units.append(unit)
    return units

def process_projects():
    """Process all projects and generate statistics"""
    os.makedirs(data_folder, exist_ok=True)
    
    # Initialize counter
    total_counter = Counter()
    proj_detail = defaultdict(lambda: {k: 0 for k in file_map})
    summary_rows = []
    
    # Process each project type
    for proj_type, pdf_name in file_map.items():
        pdf_path = os.path.join(data_folder, pdf_name)
        if not os.path.isfile(pdf_path):
            print(f'Not found: {pdf_path}, skipping')
            continue
            
        print(f'Processing {proj_type}...')
        units = extract_units(pdf_path)
        counter = Counter(units)
        
        # Save statistics for individual project types
        df = pd.DataFrame(counter.items(), columns=['Unit Name', 'Project Count'])
        df = df.sort_values('Project Count', ascending=False).reset_index(drop=True)
        df.to_csv(os.path.join(data_folder, f'{proj_type}_Project_Count.csv'),
                  index=False, encoding='utf-8-sig')
        
        # Update total counter and project details
        total_counter += counter
        summary_rows.append({'Project Type': proj_type, 'Project Count': len(units)})
        
        for unit, cnt in counter.items():
            proj_detail[unit][proj_type] = cnt
    
    # Generate total statistics table
    total_rows = []
    for unit, total in total_counter.items():
        row = {'Unit Name': unit, 'Total Project Count': total}
        row.update(proj_detail[unit])
        total_rows.append(row)
    
    total_df = pd.DataFrame(total_rows)
    cols = ['Unit Name'] + list(file_map.keys()) + ['Total Project Count']
    total_df = total_df[cols].sort_values('Total Project Count', ascending=False).reset_index(drop=True)
    total_df.to_csv(os.path.join(data_folder, 'Total_Project_Count.csv'), index=False, encoding='utf-8-sig')
    
    # Generate project type summary
    summary_rows.append({'Project Type': 'Total', 'Project Count': sum(total_counter.values())})
    pd.DataFrame(summary_rows).to_csv(os.path.join(data_folder, 'Project_Type_Summary.csv'),
                                      index=False, encoding='utf-8-sig')
    
    print('Statistics completed! Files have been output to:', data_folder)
    return total_df

# Run main function
if __name__ == "__main__":
    process_projects()

Leave a Comment