Pandas:添加具有其他列长度的列作为值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42815768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:12:27  来源:igfitidea点击:

Pandas: adding column with the length of other column as value

pythonpandas

提问by Jasper

I want to add an additional column to an existing dataframe that has the length of the 'seller_name' column as its value.

我想向现有数据框中添加一个附加列,该数据帧的值是“seller_name”列的长度。

The output should be like so:

输出应该是这样的:

seller_name    name_length
-------------|-------------
Rick         |      4
Hannah       |      6

However, I'm having difficulty getting the code right.

但是,我很难正确获取代码。

df['name_length']  = len(df['seller_name'])

just gives me the length of the entire column (6845) And

只给我整列的长度 (6845) 和

df['nl']  = df[len('seller_name')]

Throws a KeyError.

抛出一个 KeyError。

Does anyone know the correct command to achieve my goal?

有谁知道实现我的目标的正确命令?

Many thanks!

非常感谢!

回答by root

Use the .strstring accessor to perform string operations on DataFrames. In particular, you want .str.len:

使用.str字符串访问器对 DataFrame 执行字符串操作。特别是,您想要.str.len

df['name_length']  = df['seller_name'].str.len()

The resulting output:

结果输出:

  seller_name  name_length
0        Rick            4
1      Hannah            6

回答by everestial007

Say you have this data:

假设您有以下数据:

y_1980 = pd.read_csv('y_1980.csv', sep='\t')

     country  y_1980
0     afg     196
1     ago     125
2     alb      23

If you want to calculate the length of any column you can use:

如果要计算任何列的长度,可以使用:

y_1980['length'] = y_1980['country'].apply(lambda x: len(x))
print(y_1980)

     country  y_1980  length
 0     afg     196       3
 1     ago     125       3
 2     alb      23       3

This way you can calculate the length of any columns you desire.

通过这种方式,您可以计算所需的任何列的长度。