将Excel行连接成一个字符串,以在pandas DataFrame中使用。

huangapple go评论69阅读模式
英文:

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')
  1. Join all rows in excel sheet using first_sheet = '|'.join(file)

  2. 使用以下方法连接Excel工作表中的所有行:first_sheet = '|'.join(file)

  3. Using :

  4. 使用:

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')
  1. Join all rows in excel sheet using first_sheet = '|'.join(file)

  2. 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'

huangapple
  • 本文由 发表于 2020年1月6日 15:49:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定