pandas dataframe字符串格式(访问给定的列) [英] pandas dataframe string formatting (access a given column)

查看:190
本文介绍了 pandas dataframe字符串格式(访问给定的列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用新样式的格式来显示给定的/指定列的条目:

  np.random。 seed(1234)
df = pd.DataFrame(np.random.randint(7,size =(2,2)),columns = ['a','b'])
c = df。 (0):#行号0
print(Here is {one [0]} and {two}。格式(one = c,two = c ['b']))

但是我想这样做:

  print(这里是{one ['a']}和{two}。格式(one = c,two = c ['b']))# #不幸的是KeyError:'a'

有没有可能做到这一点? >

解决方案

我想你可以在中删除​​ ['a']

  print(Here is {one [a]} and {格式(one = c,two = c ['b']))
这里是3和6


I try to use new-style formatting to display the entry at a given/specified column:

np.random.seed(1234)
df = pd.DataFrame(np.random.randint(7, size=(2, 2)), columns=['a', 'b'])
c = df.iloc[0, :] # get row number 0
print("Here is {one[0]} and {two}".format(one=c, two=c['b'])) # Ok

But I'd like to do it as follows:

print("Here is {one['a']} and {two}".format(one=c, two=c['b'])) ## Unfortunately KeyError: "'a'"

Is it possible to do that and how?

解决方案

I think you can remove '' in one['a']:

print("Here is {one[a]} and {two}".format(one=c, two=c['b']))
Here is 3 and 6

这篇关于 pandas dataframe字符串格式(访问给定的列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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