以数据帧形式打印列表

2024-06-01 10:38:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图打印一个熊猫数据帧格式的二维列表。你知道吗

将数据打印为数据帧的结果

Pandas Data Frame

我的代码

cols = ["prod_id", "description", "cost"]

data = [["p01", "Domaxx Geniune Leather RFID Blocking Trifold Wallets-Made Genuine Soft Leather Large Classic Pocket Wallet,Holding 9 Cards Photo ID Coin Pocket and 2 Note compartments-Black Surface/Orange Inner", "10.00"],
["p02","Neck Wallet, Passport Holder with RFID Blocking Anti-Theft Travel Pouch Security Wallet for Credit Cards and Passport - Silver","15.00"]]

temp_str = ''

for item in cols :

    temp_str += "\t " + item

print(temp_str)

i = 0

for row in data :

    print(str(i) + "\t" + row[0] + "\t" + row[1] + "\t" + row[2])
    i += 1

=============

打印结果

normal List


Tags: and数据fordatablockingtemppocketrfid
1条回答
网友
1楼 · 发布于 2024-06-01 10:38:57

我不确定我是否理解你的意见,但如果你想要:

  prod_id                                        description   cost
0     p01  Domaxx Geniune Leather RFID Blocking Trifold W...  10.00
1     p02  Neck Wallet, Passport Holder with RFID Blockin...  15.00

发件人:

cols = ["prod_id", "description", "cost"]
data = [["p01", "Domaxx Geniune Leather RFID Blocking Trifold Wallets-Made Genuine Soft Leather Large Classic Pocket Wallet,Holding 9 Cards Photo ID Coin Pocket and 2 Note compartments-Black Surface/Orange Inner", "10.00"], ["p02","Neck Wallet, Passport Holder with RFID Blocking Anti-Theft Travel Pouch Security Wallet for Credit Cards and Passport - Silver","15.00"]]

就这么做吧:

import pandas as pd
df = pd.DataFrame(data, columns=cols)

编辑

我将向您展示一个具有较短列字段的示例:

data = [["p01", "Domaxx Geniune Leather RFID Blocking Trifold Wallets-Made", "10.00"],
["p02","Neck Wallet, Passport Holder with RFID Blocking Anti-Theft Travel Pouch Security Wallet f","15.00"]]


fmt = '{:<4}{:<10}{:<100}{}'
data1 = map(list, zip(*data))

print(fmt.format('', "prod_id", "description", "cost")) # your columns here
for i, (x, y, z) in enumerate(zip(data1[0], data1[1], data1[2])):
    print(fmt.format(i, x, y, z))

您可以使用格式的值来达到最适合您的结果

相关问题 更多 >