[英]How can I apply a function to every other row in a series in Pandas?
I have a table with only one column.我有一张只有一列的表。 I want to apply a function that I wrote to every other row in the series.我想申请一个 function 我写到该系列的每隔一行。 However, when I do that I get an error!但是,当我这样做时,我得到一个错误!
The table looks like this: And I want to get this:
names names
bank account bank account|bank|account
1256864 1256864
bank share bank share|bank|share
42,566 42,566
bank currency bank currency|bank|currency
Dollar Dollar
batch number batch number|batch|number
001444 001444
... ...
Here is the code that I wrote:这是我写的代码:
import pandas as pd
import re
df = pd.read_table('list_a.tsv')
def sep_rows (text):
sperated = '|'.join(re.split(r'\s+', text))
return text+'|'+sperated
# this applies the function to ALL rows!
print(df['names'].apply(sep_rows))
# I tried to choose every other row
a = df.iloc[::2].apply(sep_rows)
print(a) # But I gen an error!
And I get this:我明白了:
TypeError: expected string or bytes-like object
Your approach (with re
and apply
) is overcomplicated and slow.您的方法(使用re
和apply
)过于复杂且缓慢。 The following expression uses native Pandas vectorization and is much more efficient (it runs about 4 times faster).以下表达式使用原生 Pandas 向量化并且效率更高(运行速度大约快 4 倍)。
evens = df['names'].iloc[::2]
evens[:] = evens + '|' + evens.str.replace('\s+', '|')
# names
#0 bank account|bank|account
#1 1256864
#2 bank share|bank|share
#3 42,566
Treat text as a series, and then your function should work:将文本视为一个系列,然后您的 function 应该可以工作:
def sep_rows(text):
separated = text.str.replace(r"\s+", "|")
return text + "|" + separated
df.iloc[::2].apply(sep_rows)
names
0 bank account|bank|account
2 bank share|bank|share
4 bank currency|bank|currency
6 batch number|batch|number
Another way to get your result is list comprehension
:获得结果的另一种方法是list comprehension
:
import re
df['new_column'] = ["|".join((text, re.sub(r"\s+", "|", text)))
if num%2 ==0 else text
for num, text in enumerate(df.names)
]
df
names new_column
0 bank account bank account|bank|account
1 1256864 1256864
2 bank share bank share|bank|share
3 42,566 42,566
4 bank currency bank currency|bank|currency
5 Dollar Dollar
6 batch number batch number|batch|number
7 001444 001444
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.