简体   繁体   English

pandas-从另一个df元素的split元素更新数据框中的值

[英]pandas - update value in dataframe from split element of another df element

Python newb learning Pandas, so this is likely not the correct way to approach this. Python newb正在学习Pandas,因此这可能不是解决此问题的正确方法。

In dataframe df , have columns testID and proj . 在数据帧df ,具有列testIDproj

testID is an int (eg '23'), and proj is an underscore_separated_name with 3 parts (eg bob_xp_sam ). testID是一个int (例如'23'),而proj是一个由3个部分组成的underscore_separated_name(例如bob_xp_sam )。 I wish to modify all testID values to become, for eg., xp-23 (by capturing the "xp" from proj and concatinating it to each existing testID value.) 我希望将所有testID值修改为例如xp-23 (通过从proj捕获"xp"并将其隐化为每个现有的testID值)。

This is where I am at, but it doesn't fly: 这是我所在的位置,但不会飞:

df['testID'] = df['proj'].str.split('_')[1] +'-'+ df['testID']

I have tried variations, such as: 我尝试了多种形式,例如:

df['proj'].split('_')[1]
 > AttributeError: 'Series' object has no attribute 'split'

df['proj'].str.split('_')[1]
> TypeError: can only concatenate list (not "str") to list

Pretty sure my approach is wrong - a little direction would be appreciated. 可以肯定的是我的方法是错误的-会有所帮助的。

You need another str accessor before [1] to access individual element in the list type column (returned by split ): [1]之前,您需要另一个str访问器来访问列表类型列中的单个元素(由split返回):

df['proj'].str.split('_').str[1]
#                         ^^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM