Your question is a bit confusing. So, as much as I understood from your examples, for each sample of list_asm, you want to extract the very first word from the string.
The thing you are doing wrong is treating the string as a list. That is, ['uncomisd xmm2, xmm2', 'jp 0x40', ...]
is considered as a string pythonby python, not a list.
Thus, you need to extract the strings from your list first, then you can't take the first words from all these strings.
To achieve that, you can use a regular expression to find all the strings that are inside of quotes '...'
.
import pandas as pd
import re
# Read the file into dataframe
dataFrame = pd.read_json("dataset.json", lines=True)
# First extract the strings the take the first word of each string
dataFrame['opcodes'] = dataFrame['lista_asm'].apply(lambda x: [i.split()[0] for i in re.findall("'([^']*)'", x)])
print(dataFrame)
or modular form of the code would be:
import pandas as pd
import re
# Function to extract the first words from each string
def extractFirstWord(str):
listOfWords = re.findall("'([^']*)'", str)
return [i.split()[0] for i in listOfWords]
# Read the file into dataframe
dataFrame = pd.read_json("dataset.json", lines=True)
dataFrame['opcodes'] = dataFrame['lista_asm'].apply(lambda x: extractFirstWord(x))
print(dataFrame)
The result: