【发布时间】: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