【问题标题】:Lambda function applying length function to Dataframe gives different output将长度函数应用于 Dataframe 的 Lambda 函数给出不同的输出
【发布时间】:2021-05-23 09:50:09
【问题描述】:

我有一个 Pandas 数据框: 我想计算行索引 = 4 中每个单元格的长度。 我执行了 2 个命令,都给出了不同的答案。有人可以解释这里发生了什么

    a   b   c   d   e   f   g
0   1   2   3   4   5   6   first
1   3   4   5   6   7   8   second
2   6   7   8   9   10  11  third
3   first   second  third   fourth  fifth   sixth   fourth
4   column  column  column  column  column  column  fifth

First Command:
**df2.loc[4].apply(lambda x: len(x))**
Output:
a    6
b    6
c    6
d    6
e    6
f    6
g    5
Name: 4, dtype: int64

Second Command:
**df2.loc[4:].apply(lambda x: len(x))**
Output:
a    1
b    1
c    1
d    1
e    1
f    1
g    1
dtype: int64

  • Python 新手

【问题讨论】:

  • 欢迎来到 Stack Overflow!请以我们可以使用的方式发布您的数据框,请参阅此page 寻求帮助。
  • @joao:谢谢。我现在格式化了。我觉得更好看

标签: pandas lambda string-length


【解决方案1】:

造成这种差异的原因是调用df2.loc[4] 会产生Series(包含7 个元素),而调用df2.loc[4:]4:切片)会产生DataFrame (1 行 7 列)。尝试在两者上使用print()type() 看看有什么区别。

  • 在系列上,apply 会在系列的值上调用您的函数,因此您确实得到了您想要的字符串值的长度。

  • 在 DataFrame 上,apply 在每一列上调用你的函数,计算列中元素(行)的数量,它是 1,因为 [4:]slice 定义了一行(试试 [2:5],你' 将得到 3 而不是 1)。

顺便说一句,您的 lambda 函数与 len 函数相同,因此您只需编写 .apply(len)

【讨论】:

  • 感谢您的帮助 现在我很清楚了。如果我想迭代地检查数据帧的每个单元格的长度,还有一件事。那么解决方案是什么@joao
  • df2.applymap(len)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-22
  • 2021-12-23
  • 2019-04-03
相关资源
最近更新 更多