我正在尝试从列表创建数据框。 列表的内容是这样的:
[Position(account='DUC00074', contract=Stock(conId=333970434, symbol='ESGV', exchange='BATS', currency='USD', localSymbol='ESGV', tradingClass='ESGV'), position=1142.0, avgCost=52.7765556),
Position(account='DUC00074', contract=Stock(conId=238760476, symbol='ESGD', exchange='NASDAQ', currency='USD', localSymbol='ESGD', tradingClass='NMS'), position=954.0, avgCost=64.1715282),
Position(account='DUC00074', contract=Stock(conId=26653236, symbol='DVY', exchange='NASDAQ', currency='USD', localSymbol='DVY', tradingClass='NMS'), position=292.0, avgCost=101.06176745),
Position(account='DUC00074', contract=Stock(conId=27638087, symbol='VO', exchange='ARCA', currency='USD', localSymbol='VO', tradingClass='VO'), position=50.0, avgCost=170.48881),
Position(account='DUC00074', contract=Stock(conId=27684036, symbol='VDE', exchange='ARCA', currency='USD', localSymbol='VDE', tradingClass='VDE'), position=99.0, avgCost=81.962902),
Position(account='DUC00074', contract=Stock(conId=225020107, symbol='TFI', exchange='ARCA', currency='USD', localSymbol='TFI', tradingClass='TFI'), position=1109.0, avgCost=50.50644005),
Position(account='DUC00074', contract=Stock(conId=43645865, symbol='IBKR', exchange='NASDAQ', currency='USD', localSymbol='IBKR', tradingClass='NMS'), position=2800.0, avgCost=39.4058383),
Position(account='DUC00074', contract=Stock(conId=26787475, symbol='TIP', exchange='ARCA', currency='USD', localSymbol='TIP', tradingClass='TIP'), position=207.0, avgCost=115.00465265),
Position(account='DUC00074', contract=Stock(conId=46577293, symbol='BWX', exchange='ARCA', currency='USD', localSymbol='BWX', tradingClass='BWX'), position=712.0, avgCost=28.57725195),
Position(account='DUC00074', contract=Stock(conId=15547816, symbol='LQD', exchange='ARCA', currency='USD', localSymbol='LQD', tradingClass='LQD'), position=99.0, avgCost=124.4389121),
Position(account='DUC00074', contract=Stock(conId=313845618, symbol='ESML', exchange='BATS', currency='USD', localSymbol='ESML', tradingClass='ESML'), position=309.0, avgCost=27.38696345),
Position(account='DUC00074', contract=Stock(conId=31230302, symbol='VNQ', exchange='ARCA', currency='USD', localSymbol='VNQ', tradingClass='VNQ'), position=137.0, avgCost=88.7322321),
Position(account='DUC00074', contract=Stock(conId=238760480, symbol='ESGE', exchange='NASDAQ', currency='USD', localSymbol='ESGE', tradingClass='NMS'), position=1221.0, avgCost=33.994677),
Position(account='DUC00074', contract=Stock(conId=338719585, symbol='EAGG', exchange='ARCA', currency='USD', localSymbol='EAGG', tradingClass='EAGG'), position=1280.0, avgCost=53.3022901),
要创建列表,请使用:
df = pd.DataFrame(dict)
但是它会像这样返回df:
我应该怎么做才能以正确的方式可视化df中的所有信息,以便以后只选择我需要的列?
正确地=每个逗号,一列。
非常感谢您的帮助。
答案 0 :(得分:0)
尝试:
cols=[el.split("=")[0] for el in df['contract'].iloc[0].split(",")]
for el in cols:
df[el]=""
df[cols]=df['contract'].str.split(",")
df[cols]=[df[col].str.split("=").apply(lambda x: x[1]) for col in cols]
答案 1 :(得分:0)
使用:
t = [{k: v for k, v in {**x.contract._asdict(), **x._asdict()}.items()
if k != 'contract'} for x in L]
df = pd.DataFrame(t)
print (df.head())
conId symbol exchange currency localSymbol tradingClass account \
0 333970434 ESGV BATS USD ESGV ESGV DUC00074
1 238760476 ESGD NASDAQ USD ESGD NMS DUC00074
2 26653236 DVY NASDAQ USD DVY NMS DUC00074
3 27638087 VO ARCA USD VO VO DUC00074
4 27684036 VDE ARCA USD VDE VDE DUC00074
position avgCost
0 1142.0 52.776556
1 954.0 64.171528
2 292.0 101.061767
3 50.0 170.488810
4 99.0 81.962902
如果列的顺序很重要,请使用:
t = []
for x in L:
out = {}
d = x._asdict()
for k, v in d.items():
if isinstance(v, tuple):
d1 = v._asdict()
for k1, v1 in d1.items():
out[k1] = v1
else:
out[k] = v
t.append(out)
df = pd.DataFrame(t)
print (df.head())
account conId symbol exchange currency localSymbol tradingClass \
0 DUC00074 333970434 ESGV BATS USD ESGV ESGV
1 DUC00074 238760476 ESGD NASDAQ USD ESGD NMS
2 DUC00074 26653236 DVY NASDAQ USD DVY NMS
3 DUC00074 27638087 VO ARCA USD VO VO
4 DUC00074 27684036 VDE ARCA USD VDE VDE
position avgCost
0 1142.0 52.776556
1 954.0 64.171528
2 292.0 101.061767
3 50.0 170.488810
4 99.0 81.962902
设置:
L = [Position(account='DUC00074', contract=Stock(conId=333970434, symbol='ESGV', exchange='BATS', currency='USD', localSymbol='ESGV', tradingClass='ESGV'), position=1142.0, avgCost=52.7765556),
Position(account='DUC00074', contract=Stock(conId=238760476, symbol='ESGD', exchange='NASDAQ', currency='USD', localSymbol='ESGD', tradingClass='NMS'), position=954.0, avgCost=64.1715282),
Position(account='DUC00074', contract=Stock(conId=26653236, symbol='DVY', exchange='NASDAQ', currency='USD', localSymbol='DVY', tradingClass='NMS'), position=292.0, avgCost=101.06176745),
Position(account='DUC00074', contract=Stock(conId=27638087, symbol='VO', exchange='ARCA', currency='USD', localSymbol='VO', tradingClass='VO'), position=50.0, avgCost=170.48881),
Position(account='DUC00074', contract=Stock(conId=27684036, symbol='VDE', exchange='ARCA', currency='USD', localSymbol='VDE', tradingClass='VDE'), position=99.0, avgCost=81.962902),
Position(account='DUC00074', contract=Stock(conId=225020107, symbol='TFI', exchange='ARCA', currency='USD', localSymbol='TFI', tradingClass='TFI'), position=1109.0, avgCost=50.50644005),
Position(account='DUC00074', contract=Stock(conId=43645865, symbol='IBKR', exchange='NASDAQ', currency='USD', localSymbol='IBKR', tradingClass='NMS'), position=2800.0, avgCost=39.4058383),
Position(account='DUC00074', contract=Stock(conId=26787475, symbol='TIP', exchange='ARCA', currency='USD', localSymbol='TIP', tradingClass='TIP'), position=207.0, avgCost=115.00465265),
Position(account='DUC00074', contract=Stock(conId=46577293, symbol='BWX', exchange='ARCA', currency='USD', localSymbol='BWX', tradingClass='BWX'), position=712.0, avgCost=28.57725195),
Position(account='DUC00074', contract=Stock(conId=15547816, symbol='LQD', exchange='ARCA', currency='USD', localSymbol='LQD', tradingClass='LQD'), position=99.0, avgCost=124.4389121),
Position(account='DUC00074', contract=Stock(conId=313845618, symbol='ESML', exchange='BATS', currency='USD', localSymbol='ESML', tradingClass='ESML'), position=309.0, avgCost=27.38696345),
Position(account='DUC00074', contract=Stock(conId=31230302, symbol='VNQ', exchange='ARCA', currency='USD', localSymbol='VNQ', tradingClass='VNQ'), position=137.0, avgCost=88.7322321),
Position(account='DUC00074', contract=Stock(conId=238760480, symbol='ESGE', exchange='NASDAQ', currency='USD', localSymbol='ESGE', tradingClass='NMS'), position=1221.0, avgCost=33.994677),
Position(account='DUC00074', contract=Stock(conId=338719585, symbol='EAGG', exchange='ARCA', currency='USD', localSymbol='EAGG', tradingClass='EAGG'), position=1280.0, avgCost=53.3022901)]