This is my dataframe:
JavaScript
x
4
1
repository,sha1,url,refactorings
2
repo1,1,url1,"[{'type': 'Add Parameter', 'description': 'Add Parameter id : String in method public IssueFilter(repository Repository, id String) from class com.github.pockethub.android.core.issue.IssueFilter', 'leftSideLocations': [{'filePath': 'path2'}]]
3
repo2,2,url2,"[{'type': 'Add Parameter', 'description': 'Add Parameter id : String in method public IssueFilter(repository Repository, id String) from class com.github.pockethub.android.core.issue.IssueFilter', 'leftSideLocations': [{'filePath': 'path2'}]]
4
I want to extract from refactorings column : Add parameter which is the type and com.github.pockethub.android.core.issue.IssueFilter which is after from class and put them into a new column and then delete refactorings column.
The Wanted datframe is:
JavaScript
1
4
1
repository,sha1,url,refac, class
2
repo1,1,url1,Add Parameter, com.github.pockethub.android.core.issue.IssueFilter
3
repo2,2,url2,Add Parameter, com.github.pockethub.android.core.issue.IssueFilter
4
this is my code:
JavaScript
1
19
19
1
df= pd.read_csv('data.csv', sep=',')
2
3
df1 = df[['sha1','url','refactorings']]
4
df1['refac']=df.refactorings.str.extract(r'[C|c]lasss*([^ ]*)')
5
df1['class']=df.refactorings.str.extract(r"type':'s*([^ ]*)")
6
del df1['refactorings']
7
a=df1.loc[~df1.sha1.duplicated(keep='last')]
8
9
list=[]
10
for elm in a['sha1']:
11
list.append(elm)
12
dicts = {key: d for key, d in df.groupby('sha1')}
13
lenght=len(list)
14
for i in range(lenght):
15
output1="output"+str(i)+".csv"
16
a=dicts[list[i]]
17
m=pd.DataFrame.from_dict(a)
18
m.to_csv(output1, index=False, na_rep='NaN')
19
It did not extract correctly the refac and class: For the refac it return 'Add
and for the class it return com.github.pockethub.android.core.issue.IssueFilter',
also it did not create any new column and it did not delete refactorings column!
Advertisement
Answer
use regexp with str.extract()
JavaScript
1
7
1
obj = df['refactorings'].astype(str)
2
3
df['refac'] = obj.str.extract("'type': '(.*?)'")
4
df['class'] = obj.str.extract("from class (.*?)'")
5
6
df[['repository', 'sha1', 'url', 'refac', 'class']]
7