-
Notifications
You must be signed in to change notification settings - Fork 0
/
tonodirection_beta2.py
73 lines (68 loc) · 2.77 KB
/
tonodirection_beta2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2018-06-12
# @Author : imcake ([email protected])
# @Link : https://github.com/imcake
import os
import csv
import pandas as pd
######################################################
# 需要修改部分
# hasdirection --> 有向的文件名(不含扩展名)
# from_header --> 有向流起点列名
# to_header --> 有向流终点列名
# vol_header --> 有向流流量列名
######################################################
hasdirection = 'from_to_dy_jz_latlon'
from_header = 'from_id'
to_header = 'to_id'
vol_header = 'total'
######################################################
# 需要修改部分结束,结果输出csv文件名为原文件名加“nodirection”
######################################################
nodirection = hasdirection + '_nodirection'
has_file = hasdirection + '.csv'
no_file = nodirection + '.csv'
df = pd.read_csv(has_file)
from_id = df[from_header].unique()
to_id = df[to_header].unique()
result_df = df.copy()
result_df.drop(result_df.index, inplace=True)
itemList = [] # 存储OD起终点,用于判断是否已经计算过
for from_item in from_id:
for to_item in to_id:
if from_item != to_item:
forward = df.loc[(df[from_header] == from_item)
& (df[to_header] == to_item)]
forward_item = str(from_item) + str(to_item)
if forward_item in itemList:
continue
elif forward.empty:
backward = df.loc[(df[from_header] == to_item) &
(df[to_header] == from_item)]
backward_item = str(to_item) + str(from_item)
if backward_item in itemList:
continue
elif backward.empty:
continue
else:
itemList.append(backward_item)
result_df = pd.concat([result_df, backward])
else:
itemList.append(forward_item)
vol_1 = forward[vol_header].values[0]
backward = df.loc[(df[from_header] == to_item) &
(df[to_header] == from_item)]
backward_item = str(to_item) + str(from_item)
if backward_item in itemList:
continue
elif backward.empty:
continue
else:
itemList.append(backward_item)
vol_2 = backward[vol_header].values[0]
vol_all = int(vol_1) + int(vol_2)
forward[vol_header] = vol_all
# result_df.append(forward, ignore_index=True)
result_df = pd.concat([result_df, forward])
result_df.to_csv(no_file, index=False, header=True)