1

I formed a dataset, where the two columns are edge lists taken from another dataset and I mistakenly formed one of the columns as an Int64Index type when extracting the indexes as pictured here

I am trying to extract the numbers from each cell, but run into problems. When I try to handle the number as a string using the int() command, I get an error that 'DataFrame' object is not callable. However when I try to use a pandas dataframe commmand, such as to_numeric(), I get a AttributeError: 'str' object has no attribute 'to_numeric'.


df1 = pd.DataFrame(np.array(["boo","foo","bar"]),columns=['col1'])
d = {'col1': ["boo","boo","boo","bar","foo","bar"], 'Title': ["no","yes","stop","yes","stop","go"],'Example': ["p","y","x","f","v","g"] }
df2 = pd.DataFrame(data=d)
d = {'Example': ["p","y","x","f","v","g"], 'Title': ["no","yes","stop","yes","stop","go"]}
df3 = pd.DataFrame(data=d)

for i in range(0,len(df1)):
    val = df1['col1'][i]
    stuff = df2[df2["col1"] == val]
    stuff = stuff.reset_index()
    for k in range(0,len(stuff)):
      s = stuff["Example"][k]
      p = stuff["Title"][k]
      j = df2.index[(df2['Example'] == s) & (df2['Title'] == p)]
      edges = edges.append({'source': i, 'target': j}, ignore_index=True)
m = edges['target'].tolist()[3]
print(m)

output

pattern = re.compile("[\d*]")
print(type("".join(pattern.findall(m)[2:-2])))

"".join(pattern.findall(m)[2:-2])

output

int("".join(pattern.findall(m)[2:-2]))

output

Molls
  • 41
  • 4
  • please see [how to ask pandas questions](https://stackoverflow.com/a/20159305/3620003) and create a [mcve] – timgeb Mar 10 '22 at 10:49
  • 1
    I am struggling to make a reproducible example, since getting the dataframe to be like that was an accident to begin with. I will try – Molls Mar 10 '22 at 10:54
  • You can just post the output of `df.head().to_dict()` and the result you want – timgeb Mar 10 '22 at 10:54
  • 1
    I was able to recreate a sample dataset, demonstrating how the dataframe was created – Molls Mar 10 '22 at 11:15

0 Answers0