【发布时间】:2021-12-31 08:16:44
【问题描述】:
我想获取一个列表的值(使用 pandas - xlsx 创建)并将它们放入一类对象/实例中。
我当前的代码如下:
import pandas as pd
import os
cwd = os.path.abspath('')
files = os.listdir(cwd)
class Asset():
""" class to create assets """
def __init__(self, hostname, serial_number, city):
self.hostname = hostname
self.serial_number = serial_number
self.city = city
df = pd.read_excel('some_assets.xlsx', sheet_name='Tabelle1')
df1 = df[['hostname', 'serial_number', 'city']]
potions = df1.values.tolist()
potion_instances = []
for p in potions:
print (p)
potion_instances.append(Asset(*p))
列表的输出如下所示:
['host1', 2312312, 'New York']
['host2', 423213, 'New York']
['host3', 24313213, 'Stuttgart']
['host4', 3213213, 'Rom']
['host5', 1542312, 'Rom']
['host6', 42112421, 'Stuttgart']
如果我手动调用该类,例如:
test = Asset("host1", "2312312", "New York") 它有效。但不是通过使用列表。没有错误信息。有人可以帮我吗?我的错在哪里?
输入数据框:
hostname serial_number city
0 host1 2312312 New York
1 host2 423213 New York
2 host3 24313213 Stuttgart
3 host4 3213213 Rom
4 host5 1542312 Rom
5 host6 42112421 Stuttgart
【问题讨论】:
-
请提供作为数据框构造函数的数据框示例
-
嗨,我不确定我是否得到你的问题。如果我做“打印(df1)”输出是:
hostname serial_number city 0 host1 2312312 New York 1 host2 423213 New York 2 host3 24313213 Stuttgart 3 host4 3213213 Rom 4 host5 1542312 Rom 5 host6 42112421 Stuttgart
标签: python pandas list class instance