英文:
Joining excel rows to a single string to be used in pandas DataFrame
问题
I am new to Pandas.
我是Pandas的新手。
I have and excel file with 10 sheets in it. I am trying to achieve this.
我有一个包含10个工作表的Excel文件。我正试图实现这个目标。
As no answers were provided on that question I am going to use this method to check if a string in a DataFrame row contains a word from excel sheet:
由于该问题没有提供答案,我将使用以下方法来检查DataFrame行中的字符串是否包含Excel工作表中的单词:
file = pd.read_excel(open('config_values.xlsx', 'rb'),
sheet_name='ContainsFree')
-
Join all rows in excel sheet using
first_sheet = '|'.join(file)
-
使用以下方法连接Excel工作表中的所有行:
first_sheet = '|'.join(file)
-
Using :
-
使用:
df['Contains Language'] = df.Search_Query.str.contains(first_sheet, regex=True)
However, when I use '|'.join(file)
I get the first row of the excel sheet rather than the joined string:
然而,当我使用'|'.join(file)
时,我得到的是Excel工作表的第一行,而不是连接后的字符串:
excel_sheet_1
gratuit
free
gratis
...
After '|'join.(file)
I get:
gratuit
Expected:
gratuit|free|gratis
What am I doing wrong in order to join all rows in an excel sheet?
我在连接Excel工作表中的所有行方面做错了什么?
Thank you for your suggestions.
感谢您的建议。
英文:
I am new to Pandas.
I have and excel file with 10 sheets in it. I am trying to achieve this.
As no answers were provided on that question I am going to use this method to check if a string in a DataFrame row contains a word from excel sheet:
file = pd.read_excel(open('config_values.xlsx', 'rb'),
sheet_name='ContainsFree')
-
Join all rows in excel sheet using
first_sheet = '|'.join(file)
-
Using :
df['Contains Language'] = df.Search_Query.str.contains(first_sheet, regex=True)
However, when I use '|'.join(file)
I get the first row of the excel sheet rather than the joined string:
excel_sheet_1
gratuit
free
gratis
...
After '|'join.(file)
I get:
gratuit
Expected:
gratuit|free|gratis
What am I doing wrong in order to join all rows in an excel sheet?
Thank you for your suggestions.
答案1
得分: 1
file = pd.read_excel('config_values.xlsx', sheet_name='ContainsFree', header=None)
'|'.join(file[0].astype(str))
'免费|免费|免费'
英文:
Try:
file = pd.read_excel('config_values.xlsx', sheet_name='ContainsFree', header=None)
'|'.join(file[0].astype(str))
'gratuit|free|gratis'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论