-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
executable file
·82 lines (61 loc) · 1.71 KB
/
tools.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
74
75
76
77
78
79
80
81
#!/usr/bin/env python
'''
1. You have a pandas data Series of strings and you want to convert it to a list of json objects.
input: pandas Series of strings
output: pandas Series of json objects
'''
import json
def ser2json(_str):
if type(_str) == float:
pass
else:
return (json.loads(_str))
'''
2. update column in dataframe with another dataframe
'''
def updatecol(_df1,_df2):
_df1.set_index('author_id',inplace=True)
_df2.set_index('retweeted_user_id',inplace=True)
_df2.update(_df1)
_df1.reset_index(inplace=True)
_df2.reset_index(inplace=True)
return _df2
def float2int(_str):
if type(_str) == None:
pass
else:
return (int(_str))
'''
reading when converting data
'''
import pandas as pd
in_reply_user = pd.read_csv('../Project6/data/in_reply_to_screen_name.csv',converters={'Unnamed: 0': str})
'''
domains extractor
'''
import tldextract
def domain4(_url):
if type(_url) == str:
_ext = tldextract.extract(_url)
return _ext.registered_domain
else:
return None
def concatdf(path,files):
DF=pd.DataFrame()
for i in range(len(files)):
each = files[i]
df=pd.read_csv(path+each)
print(df.shape)
DF=pd.concat([DF,df])
return DF
## concat 'text by username
df.groupby(by='name').agg(text=("text", lambda x: ",".join(set(x))))
df1['day'] = np.where(df1['name'].isin(df2['name']), df1['name'].map(df2.set_index('name')['day']),df1['day'])
def batch_file(array,n_workers):
file_len = len(array)
batch_size = round(file_len / n_workers)
batches = [
array[ix:ix+batch_size]
for ix in tqdm(range(0, file_len, batch_size))
]
return batches