我可以在 excel 单元格中写一行代码,然后将其导入到 python 中吗?
Can I write a line of code in an excel cell and then import it into python?
我在 excel 单元格中写了一行代码。我将它作为 df 导入 python。它有 1 个调用 df['column']。它把它当作一个字符串,而不是从我正在寻找的列中调用。当我将导入的 df 合并到新的 df 时,我希望字符串为 'activate'。
导入的 df 包含类似... 'This is part of the' + df['adjective'] + '我要导入的字符串。'所以写成代码,我就是想让它起到代码的作用。
原来的df有一栏,'adjective'。当我将它合并到新的数据框时,我希望字符串适应新的数据框。但是整个单元格被视为一个长字符串。有没有办法让字符串像我在 python?
中写的代码一样工作
我知道还有其他更迂回的方法可以让我的判决生效。我已将其与 .replace 一起使用,但它没有我想要的那么灵活。
df1 = pd. DataFrame({'adjective': ['small','Big'], 'key': [1,2]})
imported_df = pd.DataFrame({'key':[1,2], 'sentence': ['random words' + df2['adjective'] + 'problem', 'other random string' + df2['adjective'] + 'random words']})
df2 = df1.merge(imported_df[['key]])
我得到这个字符串,'random words' + df1['adjective'] + 'problem'.
我想要 'random words small problem' 或 'other random string Big random words'。但它没有读取 df1['adjective'] 的调用。它把它留在字符串中。
您可以使用 openpyxl 包来读取 excel 特定单元格,或者如果代码更有条理,您可以使用 pandas read_excel() 来读取代码作为字符串或一系列字符串,那么,你可以使用题目here到运行代码中提出的解决方案。
我强烈建议使用 try 和 except 来 运行 代码,如果有任何错误,请调试每个代码。
我在 excel 单元格中写了一行代码。我将它作为 df 导入 python。它有 1 个调用 df['column']。它把它当作一个字符串,而不是从我正在寻找的列中调用。当我将导入的 df 合并到新的 df 时,我希望字符串为 'activate'。
导入的 df 包含类似... 'This is part of the' + df['adjective'] + '我要导入的字符串。'所以写成代码,我就是想让它起到代码的作用。
原来的df有一栏,'adjective'。当我将它合并到新的数据框时,我希望字符串适应新的数据框。但是整个单元格被视为一个长字符串。有没有办法让字符串像我在 python?
中写的代码一样工作我知道还有其他更迂回的方法可以让我的判决生效。我已将其与 .replace 一起使用,但它没有我想要的那么灵活。
df1 = pd. DataFrame({'adjective': ['small','Big'], 'key': [1,2]})
imported_df = pd.DataFrame({'key':[1,2], 'sentence': ['random words' + df2['adjective'] + 'problem', 'other random string' + df2['adjective'] + 'random words']})
df2 = df1.merge(imported_df[['key]])
我得到这个字符串,'random words' + df1['adjective'] + 'problem'.
我想要 'random words small problem' 或 'other random string Big random words'。但它没有读取 df1['adjective'] 的调用。它把它留在字符串中。
您可以使用 openpyxl 包来读取 excel 特定单元格,或者如果代码更有条理,您可以使用 pandas read_excel() 来读取代码作为字符串或一系列字符串,那么,你可以使用题目here到运行代码中提出的解决方案。
我强烈建议使用 try 和 except 来 运行 代码,如果有任何错误,请调试每个代码。