【问题标题】:how to iterate over strings of a dataframe cell?如何迭代数据框单元格的字符串?
【发布时间】:2020-08-01 03:44:00
【问题描述】:

我有一个数据框,每个单元格中都有文本。我想遍历数据框及其单元格的单个字符,并用 0 填充一个列表来表示有一个空格,或者用 1 表示一个字符。我尝试了 itertuples、iterrows 和 iteritems,但我无法访问字符串的每个字符。

crispr = pd.DataFrame({'Name': ['Bob', 'Jane', 'Alice'], 
                       'Issue': ['Handling data', 'Could not read sample', 'No clue'],
                       'Comment': ['Need to revise data', 'sample preparation', 'could not find out where problem occurs']})

我尝试的是:

dflist = []
countchar= 0
for i,j in crispr.iteritems():
    for x in range(len(j)):
        test = j[countchar].isspace()
        countchar+=1
        if test == True:
            dflist.append(0)
        else:
            dflist.append(1)

我试图弄清楚它是否适用于 itertuples 或 iterrows():

for i in crispr.itertuples():
    for j in i:
        for b in j:
            print(b)

出现以下错误:

 TypeError: 'int' object is not iterable  

预期的输出是一个列表,其中 1 表示字符,0 表示空格:

dflist = [[1,1,1], [1,1,1,1], [1,1,1,1,1]],[[1,1,1,1,1,1,1,0,1,1,1,1], ...]]

【问题讨论】:

标签: python pandas dataframe character iterable


【解决方案1】:

您发布的代码(在您上次编辑之前)有问题,其中包含许多未知内容,导致与您发布的内容不同的错误。我将您的代码固定为:

dflist = []                    # added this
for i,j in crispr.iteritems():
    for x in range(len(j)):
        test = j[x].isspace()  # changed countchar to x
        # countchar+=1         # removed this
        if test == True:
            dflist.append(0)
        else:
            dflist.append(1)

for i in crispr.itertuples():
    for j in i:
        for b in j:  # this procudes your error
            print(b)

如果您检查 j 的第一项,您会看到它的值 0 - 因此是错误。你不能迭代0

解决方案:

import pandas as pd

crispr = pd.DataFrame({
    'Name': ['Bob', 'Jane', 'Alice'],
    'Issue': ['Handling data', 'Could not read sample', 'No clue'],
    'Comment': ['Need to revise data', 'sample preparation', 
                'could not find out where problem occurs']})

print(crispr)
outer_list = []
for i,j in crispr.iteritems():
    dflist = []
    for word in j:
        wordlist = [] 
        for char in word:
            if char.isspace():
                wordlist.append(0)
            else:
                wordlist.append(1)
        dflist.append(wordlist)
    outer_list.append(dflist)

print(outer_list)

输出(为清楚起见添加了 cmets):

                                   Comment                  Issue   Name
0                      Need to revise data          Handling data    Bob
1                       sample preparation  Could not read sample   Jane
2  could not find out where problem occurs                No clue  Alice

# Comment
[[[1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1], 
  [1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 
  [1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 
   1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1]], 
 # Issue
 [[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1], 
  [1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1], 
  [1, 1, 0, 1, 1, 1, 1]],
 # Name 
 [[1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1, 1]]]

应该做你想做的。

【讨论】:

    猜你喜欢
    • 2019-10-13
    • 2010-09-19
    • 2010-10-24
    • 2020-03-12
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 2012-09-20
    相关资源
    最近更新 更多