我有这段代码:
import numpy as np
headings = np.array(["userID","name","bookingID"])
data = np.array([[1111111,"Joe Bloggs",2222222][1212121,"Jane Doe",3333333]])
如何以这种方式打印此数据:
userID
1111111
name
Joe Bloggs
bookingID
2222222
userID
1212121
name
Jane Doe
bookingID
3333333
这是我最接近的......
keys = headings.values
datas = df.values
for i in datas:
for j in keys:
print(j)
print(i)
结果:
userID
[1111111 'Joe Bloggs' 2222222]
name
[1111111 'Joe Bloggs' 2222222]
bookingID
[1111111 'Joe Bloggs' 2222222]
userID
[1212121 'Jane Doe' 3333333]
name
[1212121 'Jane Doe' 3333333]
bookingID
[1212121 'Jane Doe' 3333333]
但我正在努力将额外的步骤缩小到每个键的具体数据......有人可以帮忙吗? (还有人可以推荐更好的问题标题吗?)
答案 0 :(得分:2)
import numpy as np
headings = np.array(["userID","name","bookingID"])
data = np.array([[1111111,"Joe Bloggs",2222222],[1212121,"Jane Doe",3333333]])
for sublist in data: #iterate through data
for ind, value in enumerate(sublist): #iterate through each sublist and remember index
print(headings[ind]) #print the element in headings that corresponds to index
print(value) #print the element
输出:
>>>userID
>>>1111111
>>>name
>>>Joe Bloggs
>>>bookingID
>>>2222222
>>>userID
>>>1212121
>>>name
>>>Jane Doe
>>>bookingID
>>>3333333