熊猫一次迭代列值一次并生成范围
我有一个大熊猫的数据框架,如下所示,
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个错误在于最内向的山地。
您是在一维值数组上要求迭代器,该迭代器会产生标量值,因此无法解开包装。
如果您的DataFrame每列只有2个项目,那么
这应该足以替代
使用
loc
pandas ,这实际上是最简单的解决方法,但直到现在才发生在我身上。我们将列名
col
与loc
一起获取所有行(给出:
给出 inloc> loc [:,, col] )
attergetter
我们可以使用
operator
库中的attergetter对象获取我们想要的单个(或尽可能多的属性):attergetter 2
此方法类似于上面的方法,除了我们选择多个多个列,有I和J在两个列表中,每个条目对应于一个列。
熊猫解决方案
这种方法仅使用熊猫功能。它使用
df1.columns.get_loc(col_name)
函数获取列索引,然后使用.iloc
索引值。在.iloc [a,b]
我们使用:代替A选择所有行,index
代替B来选择仅选择列。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
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 withloc
to get all rows (given by:
inloc[:, col]
)Attrgetter
We can use the attrgetter object from
operator
library to get a single (or as many attributes) as we want: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.
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, andindex
in place of b to select just the column.