pandas ,在字符串中打印变量 [英] Pandas, print variable in string

查看:70
本文介绍了 pandas ,在字符串中打印变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据框(新):

I have a dataframe (new) that looks something like this:

num  name1  name2
11    A      AB
14    Y      YX
25    L      LS
39    Z      ZT
....

我只想在打印语句中提取 num 值,这样我的输出看起来像这样:

and I just want to extract the num value in a print statement such that I have an output that looks like this:

The value is 11
The value is 14
The value is 25
...

我不确定这样做的正确格式是什么,因为以下代码只是迭代值是".

I'm not sure what the correct format to do this is, as the following bit of code just iterates "The value is".

 for index, row in new.iterrows():
     print('The value is').format(new['num'])

推荐答案

稍微改变你的代码

for index, row in df.iterrows():
    print('The value is {0}'.format(row['num']))
    
The value is 11
The value is 14
The value is 25
The value is 39

使用 f 字符串:

for index, row in df.iterrows():
    print(f"The value is {row['num']}")

要打印多列,使用点表示法:

To print multiple columns, using dot notation:

for index, row in df.iterrows():
    print(f"{row.name1} and {row.name2} have a value of {row.num}")

A and AB have a value of 11
Y and YX have a value of 14
L and LS have a value of 25
Z and ZT have a value of 39

这篇关于 pandas ,在字符串中打印变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆