熊猫一次迭代列值一次并生成范围

发布于 2025-02-13 01:16:30 字数 647 浏览 1 评论 0原文

我有一个大熊猫的数据框架,如下所示,

df1 = pd.DataFrame({'biz': [18, 23], 'seg': [30, 34], 'PID': [40, 52]})

我想在下面做

a)一次从每列中传递所有值,以

for ex:

我正在尝试以下操作,

cols = ['biz','seg','PID']
for col in cols:
  for i, j in df1.col.values:
      print("D" + str(i) + ":" + "F" + str(j))
      print("Q" + str(i) + ":" + "S" + str(j))
      print("AB" + str(i) + ":" + "AD" + str(j))

但这是不起作用的,我会遇到一个错误

typeError:不能解开不合时宜的numpy.int64对象

我希望我的输出如下

D18:F23
Q18:S23
AB18:AD23
D30:F34
Q30:S34
AB30:AD34
D40:F52
Q40:S52
AB40:AD52

I have a pandas dataframe like as below

df1 = pd.DataFrame({'biz': [18, 23], 'seg': [30, 34], 'PID': [40, 52]})

I would like to do the below

a) pass all the values from each column at once to for loop

For ex:

I am trying the below

cols = ['biz','seg','PID']
for col in cols:
  for i, j in df1.col.values:
      print("D" + str(i) + ":" + "F" + str(j))
      print("Q" + str(i) + ":" + "S" + str(j))
      print("AB" + str(i) + ":" + "AD" + str(j))

but this doesn;t work and I get an error

TypeError: cannot unpack non-iterable numpy.int64 object

I expect my output to be like as below

D18:F23
Q18:S23
AB18:AD23
D30:F34
Q30:S34
AB30:AD34
D40:F52
Q40:S52
AB40:AD52

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时间海 2025-02-20 01:16:30

这个错误在于最内向的山地。

您是在一维值数组上要求迭代器,该迭代器会产生标量值,因此无法解开包装。

如果您的DataFrame每列只有2个项目,那么

cols = ['biz','seg','PID']
for col in cols:
   i, j = getattr(df1, col).values
   print("D" + str(i) + ":" + "F" + str(j))
   print("Q" + str(i) + ":" + "S" + str(j))
   print("AB" + str(i) + ":" + "AD" + str(j))

这应该足以替代

使用loc

pandas ,这实际上是最简单的解决方法,但直到现在才发生在我身上。我们将列名colloc一起获取所有行(给出:给出 in loc> loc [:,, col] )

cols = ['biz','seg','PID']
for col in cols:
    i, j = df1.loc[:, col].values

attergetter

我们可以使用operator库中的attergetter对象获取我们想要的单个(或尽可能多的属性):

from operator import attrgetter

cols = ['biz','seg','PID']
cols = attrgetter(*cols)(df1)
for col in cols:
   i, j = col.values

attergetter 2

此方法类似于上面的方法,除了我们选择多个多个列,有I和J在两个列表中,每个条目对应于一个列。

from operator import attrgetter

cols = ['biz','seg','PID']
cols = attrgetter(*cols)(df1)
cols = [col.values for col in cols]
all_i, all_j = zip(*cols)

熊猫解决方案

这种方法仅使用熊猫功能。它使用df1.columns.get_loc(col_name)函数获取列索引,然后使用.iloc索引值。在.iloc [a,b]我们使用:代替A选择所有行,index代替B来选择仅选择列。

cols = ['biz','seg','PID']
for col in cols:
    index = df1.columns.get_loc(col)
    i, j = df1.iloc[:, index]
    # do the printing here

The mistake is in the innermost forloop.

You are requesting an iterator over a 1-dimensional array of values, this iterator yields scalar values and hence they can not be unpacked.

If your dataframe only has 2 items per column, then this should suffice

cols = ['biz','seg','PID']
for col in cols:
   i, j = getattr(df1, col).values
   print("D" + str(i) + ":" + "F" + str(j))
   print("Q" + str(i) + ":" + "S" + str(j))
   print("AB" + str(i) + ":" + "AD" + str(j))

Alternatives

Pandas using loc

This is actually the simplest way to solve it but only now it occurred to me. We use the column name col along with loc to get all rows (given by : in loc[:, col])

cols = ['biz','seg','PID']
for col in cols:
    i, j = df1.loc[:, col].values

Attrgetter

We can use the attrgetter object from operator library to get a single (or as many attributes) as we want:

from operator import attrgetter

cols = ['biz','seg','PID']
cols = attrgetter(*cols)(df1)
for col in cols:
   i, j = col.values

Attrgetter 2

This approach is similar to the one above, except that we select multiple columns and have the i and j in two lists, with each entry corresponding to one column.

from operator import attrgetter

cols = ['biz','seg','PID']
cols = attrgetter(*cols)(df1)
cols = [col.values for col in cols]
all_i, all_j = zip(*cols)

Pandas solution

This approach uses just pandas functions. It gets the column index using the df1.columns.get_loc(col_name) function, and then uses .iloc to index the values. In .iloc[a,b] we use : in place of a to select all rows, and index in place of b to select just the column.

cols = ['biz','seg','PID']
for col in cols:
    index = df1.columns.get_loc(col)
    i, j = df1.iloc[:, index]
    # do the printing here
治碍 2025-02-20 01:16:30
for i in range(len(df1)):
    print('D' + str(df1.iloc[i,0]) + ':' + 'F' + str(df1.iloc[i+1,0]))
    print('Q' + str(df1.iloc[i,0]) + ':' + 'S' + str(df1.iloc[i+1,0]))
    print('AB' + str(df1.iloc[i,0]) + ':' + 'AD' + str(df1.iloc[i+1,0]))
    print('D' + str(df1.iloc[i,1]) + ':' + 'F' + str(df1.iloc[i+1,1]))
    print('Q' + str(df1.iloc[i,1]) + ':' + 'S' + str(df1.iloc[i+1,1]))
    print('AB' + str(df1.iloc[i,1]) + ':' + 'AD' + str(df1.iloc[i+1,1]))
    print('D' + str(df1.iloc[i,2]) + ':' + 'F' + str(df1.iloc[i+1,2]))
    print('Q' + str(df1.iloc[i,2]) + ':' + 'S' + str(df1.iloc[i+1,2]))
    print('AB' + str(df1.iloc[i,2]) + ':' + 'AD' + str(df1.iloc[i+1,2]))
for i in range(len(df1)):
    print('D' + str(df1.iloc[i,0]) + ':' + 'F' + str(df1.iloc[i+1,0]))
    print('Q' + str(df1.iloc[i,0]) + ':' + 'S' + str(df1.iloc[i+1,0]))
    print('AB' + str(df1.iloc[i,0]) + ':' + 'AD' + str(df1.iloc[i+1,0]))
    print('D' + str(df1.iloc[i,1]) + ':' + 'F' + str(df1.iloc[i+1,1]))
    print('Q' + str(df1.iloc[i,1]) + ':' + 'S' + str(df1.iloc[i+1,1]))
    print('AB' + str(df1.iloc[i,1]) + ':' + 'AD' + str(df1.iloc[i+1,1]))
    print('D' + str(df1.iloc[i,2]) + ':' + 'F' + str(df1.iloc[i+1,2]))
    print('Q' + str(df1.iloc[i,2]) + ':' + 'S' + str(df1.iloc[i+1,2]))
    print('AB' + str(df1.iloc[i,2]) + ':' + 'AD' + str(df1.iloc[i+1,2]))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文