gpt4 book ai didi

python - 以数据帧格式打印列表

转载 作者:太空宇宙 更新时间:2023-11-03 21:04:29 27 4
gpt4 key购买 nike

我正在尝试以 pandas 数据帧格式打印二维列表。

将数据打印为 Pandas 数据框的结果

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

最佳答案

我不确定我是否理解您的输入,但如果您想要这样:

  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))

您可以调整格式的值以获得最适合您的结果

关于python - 以数据帧格式打印列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55515239/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com