数据帧中所有列中元素长度的结果是,
Dataframe is: Fruits City 0 Apple Shimla 1 Orange Sydney 2 Mango Lucknow 3 Kiwi Wellington Length of the elements in all columns Fruits City 0 5 6 1 6 6 2 5 7 3 4 10
为了解决这个问题,我们将遵循以下步骤-
定义一个数据框
使用df.applymaplambda函数内部的函数来计算所有列中元素的长度为
df.applymap(lambda x:len(str(x)))
让我们检查以下代码以获得更好的理解-
import pandas as pd df = pd.DataFrame({'Fruits': ["Apple","Orange","Mango","Kiwi"], 'City' : ["Shimla","Sydney","Lucknow","Wellington"] }) print("Dataframe is:\n",df) print("Length of the elements in all columns") print(df.applymap(lambda x:len(str(x))))
Dataframe is: Fruits City 0 Apple Shimla 1 Orange Sydney 2 Mango Lucknow 3 Kiwi Wellington Length of the elements in all columns: Fruits City 0 5 6 1 6 6 2 5 7 3 4 10