python - 比较 pandas DataFrame 列中的多个字符串

标签 python string python-3.x pandas

我在 Python3.x 中有以下 pandas DataFrame,有几个数字列和两个字符串列:

import numpy as np
import pandas as pd

dict = {"numericvals": np.repeat(25, 8), 
    "numeric":np.repeat(42, 8), 
    "first":["beneficiary, duke", "compose", "herd primary", "stall", "deep", "regular summary classify", "timber", "property”], 
    "second": ["abcde”, "abcde”, "abcde”, "abcde”, "abcde”, "abcde”, "abcde”, "abcde”]}

df = pd.DataFrame(dict1)

df = df[['numeric', 'numericvals', 'first', 'second']]

print(df)
   numeric  numericvals                     first second
0       42           25         beneficiary, duke  abcde
1       42           25                   compose  abcde
2       42           25              herd primary  abcde
3       42           25                     stall  abcde
4       42           25                      deep  abcde
5       42           25  regular summary classify  abcde
6       42           25                    timber  abcde
7       42           25                  property  abcde

first 列包含一个或多个字符串。如果不止一个,则用空格或逗号分隔。

我的目标是创建一个列来记录 first 中字符串的长度,这些字符串的长度比 second 中的字符串长或短。如果这些大小相同,则应忽略这种情况。

我的想法是创建两个列表:

longer = []
shorter = []

如果first中的字符串较长,则通过len()将字符串长度追加到longer中。如果字符串较短,则通过len()将字符串长度记录在short中。

分析应该是这样的(pandas DataFrame 格式):

   numericvals numeric                   first second  longer  shorter
0          25      42        beneficiary, duke abcde  11       4
1          25      42                  compose abcde  7        0
2          25      42             herd primary abcde  7        4
3          25      42                    stall abcde  0        0
4          25      42                     deep abcde  0        4
5          25      42 regular summary classify abcde  7, 7, 8  0
6          25      42                   timber abcde  6        0
7          25      42                 property abcde  8        0

我不知道如何在 first 中处理多个字符串,尤其是当有 3 个时。应该如何在 pandas 中进行这种比较?

最佳答案

您可以使用pandas.DataFrame.apply (source) :

import operator

def transform(df, op):
    lengths = [len(s) for s in df['first'].replace(',', ' ').split()]
    return [f for f in lengths if op(f, len(df.second))] or [0]

df['longer']  = df.apply(transform, axis=1, args=[operator.gt])
df['shorter'] = df.apply(transform, axis=1, args=[operator.lt])

这应该适用于任何数量的字符串,假设任何空格或逗号表示一个新字符串。

这是输出:

   numeric  numericvals                     first second     longer shorter
0       42           25         beneficiary, duke  abcde       [11]     [4]
1       42           25                   compose  abcde        [7]     [0]
2       42           25              herd primary  abcde        [7]     [4]
3       42           25                     stall  abcde        [0]     [0]
4       42           25                      deep  abcde        [0]     [4]
5       42           25  regular summary classify  abcde  [7, 7, 8]     [0]
6       42           25                    timber  abcde        [6]     [0]
7       42           25                  property  abcde        [8]     [0]

我尽力了。希望这对您有所帮助!

关于python - 比较 pandas DataFrame 列中的多个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49888736/

相关文章:

python - PySpark:计算按 AUC 分组

python - Python Pandas 是否有办法指定一个列来计算值组合的每次出现?

python - easy_install 下载目录

python - 验证字符串输入并将其连接到列表

string - Lua中计算字符串转换为int

javascript - 如何将数组中的每个元素与多个条件进行比较?

java - 无论字符串大小如何,如何在 Java 中获取字符串中的最后一个字符

python - 如何在 Django 查询集中使用 order_by?

python-3.x - 使用pyrfc获取FAGLL03H报告

python - 在 'for' 循环期间创建并写入新的 csv 文件