当前位置: 代码迷 >> python >> 如何在fstrings中使用.loc?
  详细解决方案

如何在fstrings中使用.loc?

热度:77   发布时间:2023-07-16 09:50:31.0

我有这样的数据帧:

import pandas as pd

df = pd.DataFrame({'col1': ['abc', 'def', 'tre'],
                   'col2': ['foo', 'bar', 'stuff']})

  col1   col2
0  abc    foo
1  def    bar
2  tre  stuff

和这样的字典:

d = {'col1': [0, 2], 'col2': [1]}

该字典包含要从数据帧中提取的列名和值索引,以生成如下字符串:

abc (0, col1)

因此,每个字符串都以元素本身开头,在括号中显示索引和列名称。

我尝试了以下列表理解:

l = [f"{df.loc[{indi}, {ci}]} ({indi}, {ci})"
     for ci, vali in d.items()
     for indi in vali]

产量

['  col1\n0  abc (0, col1)',
 '  col1\n2  tre (2, col1)',
 '  col2\n1  bar (1, col2)']

所以,几乎可以,只需要避免col1\\n0部分。

如果我试试

f"{df.loc[0, 'col1']} is great"

我明白了

'abc is great'

然而,根据需要,

x = 0
f"{df.loc[{x}, 'col1']} is great"

我明白了

'0    abc\nName: col1, dtype: object is great'

怎么能修好?

import pandas as pd

df = pd.DataFrame({'col1': ['abc', 'def', 'tre'],
                   'col2': ['foo', 'bar', 'stuff']})

d = {'col1': [0, 2], 'col2': [1]}
x = 0
[f"{df.loc[x, 'col1']} is great"
     for ci, vali in d.items()
     for indi in vali]

这给你:

['abc is great', 'abc is great', 'abc is great']

这就是你要找的?

你也可以通过x范围循环

[f"{df.loc[i, 'col1']} is great"
 for ci, vali in d.items()
 for indi in vali
 for i in range(2)]

#output
['abc is great',
 'def is great',
 'abc is great',
 'def is great',
 'abc is great',
 'def is great']

你们看到的是串表示,和丑陋的换行符\\n字符的pd.Series对象通过返回的loc acessor。

你应该使用来返回标量,并注意你的索引标签不需要嵌套{}

L = [f'{df.at[indi, ci]} ({indi}, {ci})' \
     for ci, vali in d.items() \
     for indi in vali]

print(L)

['abc (0, col1)', 'tre (2, col1)', 'bar (1, col2)']
  相关解决方案