Python 沿着panda系列进行迭代,将以字母开头的行附加到以数字开头的行的末尾
我在使用python进行for循环时遇到了一些问题。下面是一个单列数据帧示例。我发现的大多数示例都是针对一次性处理整个数据帧的。或者搜索一个单词并附加到前一行Python 沿着panda系列进行迭代,将以字母开头的行附加到以数字开头的行的末尾,python,pandas,loops,Python,Pandas,Loops,我在使用python进行for循环时遇到了一些问题。下面是一个单列数据帧示例。我发现的大多数示例都是针对一次性处理整个数据帧的。或者搜索一个单词并附加到前一行 What I am trying to do: Forgive me trying to sound it out in a logical way. 1-Start at (0,Test) in the series. 2-Check element at (0,Test) for number at first position (0
What I am trying to do: Forgive me trying to sound it out in a logical way.
1-Start at (0,Test) in the series.
2-Check element at (0,Test) for number at first position (0). If True, then hold and (store)
pre_number_line.
3-Goto next line down.
4-Check element (1,Test) for number at first position (0). If False, then check first position for
letter.
5-If first character True for letter, concatenate current line at the end of the pre_num_line or
(0,Test) position line in this case.
6-Delete current row & shift rows up.(instead maybe change string(line) to NaN and delete all NaN at
end of code). Not sure which is easier.
7-Analyze next row down at (2,Test) repeat process starting at step 2.
8-End loop when all rows with letters (at 1st position) have been appended to the pre_num_line.
9-Next row down, should start with numbers. This will be the new pre_num_line.
列出的只是字符串的开头。尽管如此,字符串可以包含数字和字母。每行的第一个位置始终是数字或字母(不区分大小写)。每个字母行必须与上面的编号行合并(在末尾)。在处理结束时,只存在编号的行
import pandas as pd
from pandas import DataFrame, Series
dat = {'Test': ['123456ab', 'coff-4', 'eat 8', 'bagle6', '345678-edh', 'wine', 'bread','567890 tfs',
'grape']}
df = pd.DataFrame(dat)
letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
numbers = '0123456789'
#-------------------
pre_num_lin = None
for line in df.Test:
if line[0] in numbers:
pre_num_lin = df['Test']
if line[0] in letters:
pre_num_lin = pre_num_lin + ' ' + line
#------------------
print(df)
What it should look like at end:
Test
0 123456ab coff-4 eat 8 bagle6
1 345678-edh wine 4 bread
2 567890 tfs grape
我感谢你们的时间和知识。如果您有任何问题,请告诉我。试试这个:
df.groupby(df['Test'].str[0].str.isnumeric().cumsum())['Test'].agg(' '.join)
输出:
Test
1 123456ab coff-4 eat 8 bagle6
2 345678-edh wine bread
3 567890 tfs grape
Name: Test, dtype: object
详情:
使用字符串访问器和零索引器获取第一个字母df['Test'].str[0]
等于df['Test'].str.get(0)
(只需少打字)
接下来,使用字符串访问器和isnumeric
方法检查该字符是否为数字。这将返回一个布尔级数
df['Test'].str[0].str.isnumeric()
0 True
1 False
2 False
3 False
4 True
5 False
6 False
7 True
8 False
Name: Test, dtype: bool
现在,我们可以使用cumsum
创建如下行分组:
df['Test'].str[0].str.isnumeric().cumsum()
0 1
1 1
2 1
3 1
4 2
5 2
6 2
7 3
8 3
Name: Test, dtype: int32
最后,我们可以使用生成分组的序列来分组,并应用stringjoin
的聚合:
df.groupby(df['Test'].str[0].str.isnumeric().cumsum())['Test'].agg(' '.join)
Test
1 123456ab coff-4 eat 8 bagle6
2 345678-edh wine bread
3 567890 tfs grape
Name: Test, dtype: object
感谢您及时回复。哇,一行。!!给人印象深刻的现在,我需要分解代码,了解它的作用。你能帮点忙吗。每个部分都做些什么。“我会很感激的。”布默说,当然。我会详细说明这里发生了什么。斯科特,谢谢你花时间来做这件事并帮助我。令人印象深刻!!我的错,我早做了,它不让我。我想我批准它的速度太快了。总之,一切都很好。再次谢谢你,拿着Care@Boomer非常感谢。保持安全和健康!