如何从另一个单元格获取数据框单元格内容

时间:2021-04-10 13:55:59

标签: python pandas dataframe

我有一个像这样的熊猫数据框:

<头>
图片 标签
图像1 现在
图像2 昨天

我需要从这个循环中获得的列“labels”中的值

for image in os.listdir('train_images'):
   if df_train['image'].str.contains(image).any():
       print('labels')

我在这里得到 image1 并且我想要列标签中的值

最终的结果必须是这样的:

<头>
现在
昨天

谢谢

1 个答案:

答案 0 :(得分:0)

在熊猫中:

df = pd.DataFrame(np.array([['image1', 'now'], ['image2', 'yesterday']]),
                   columns=['image', 'labels'])

解决方案可以很简单 - 如果只有 2 列:(resultType: Dataframe)

del df['image']
df

enter image description here

但是如果有 x 列则:(resultType: object)

onlyLabels = df['labels']
onlyLabels

enter image description here

(结果类型:字符串)

for i in df['labels']: 
    print (i)

enter image description here