pandas 带嵌套参数的数据框字符串格式(访问指定列)

pandas dataframe string formatting with nested arguments (access specified column)

这是 的后续。

而不是

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

我希望能够在嵌套参数中引用该列,但它不起作用:

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

应该可以,但没有。有什么提示吗?

正如我在对链接问题的回复中所张贴的那样,这不是您引用数据的方式。

根据 docs:

str.format(*args, **kwargs) Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces {}. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument.

>>> "The sum of 1 + 2 is {0}".format(1+2) 'The sum of 1 + 2 is 3'

最好在代码中明确说明数据。

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

或...

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

更好...

col1 = 'a'
col2 = 'b'

n = 0  # Get the first row.
one, two = df.ix[n, [col1, col2]]
>>> print("Here is {one} and {two}".format(one=one, two=two))
Here is 3 and 6

鉴于 Series 在后台支持 __getattr__,您还可以使用点表示法或类似字典查找的方式间接访问结果。

row = df.loc[n]

>>> print("Here is {row.a} and {row.b}".format(row=row))
Here is 3 and 6

尽管在任何列名称与现有系列 属性 或方法冲突的情况下,使用括号访问数据总是更安全。

df['sum'] = df.sum(axis=1)

# Safe method.
>>> print("{row[a]} and {row[b]} make {row[sum]}".format(row=row))
    3 and 6 make 9

# Unsafe method.
print("{row.a} and {row.b} make {row.sum}".format(row=row))
3 and 6 make <bound method Series.sum of a      3
b      6
sum    9
Name: 0, dtype: int64>

我认为您遇到的错误是由于 format 的嵌套替换造成的。对于这种情况,您可以混合 %format...

print(("Here is {one[%(col)s]} and {two}" % {'col':'a'}).format(one=c, two=c['b']))

Here is 3 and 6