我一直在寻找修复即将deprIcated方法from_items与建议from_dict
pd.set_option('display.max_columns', 50)
pd.set_option('display.max_rows', 50)
Test_Data = [('originating_system_id', ['CL', 'CL', 'CL', 'CL']),
('security_type1', ['CORP', 'CORP', 'CORP', 'CORP']),
('state', ['Traded', 'Covered', 'Traded Away', 'Traded']),
('trading_book', ['LCAAAAA', 'NUBBBBB', 'EDFGSFG', 'PDFEFGR'])
]
df = pd.DataFrame.from_items(Test_Data)
print(df)
originating_system_id security_type1 state trading_book
0 CL CORP Traded LCAAAAA
1 CL CORP Covered NUBBBBB
2 CL CORP Traded Away EDFGSFG
3 CL CORP Traded PDFEFGR
当我改为from_dictdf赋值时:
df = pd.DataFrame.from_dict(Test_Data)
我希望应用过滤器时出现以下行错误:
m1 = ~df['trading_book'].str.startswith(tuple(prefixes))
KeyError: 'trading_book'
是from_dict结构不同?有替代品from_items吗?
解决办法:对我来说工作很好,将其转换为字典:
df = pd.DataFrame(dict(Test_Data))
#another alternative solution
#df = pd.DataFrame({a:b for a, b in Test_Data})
print(df)
originating_system_id rbc_security_type1 state trading_book
0 CL CORP Traded LCAAAAA
1 CL CORP Covered NUBBBBB
2 CL CORP Traded Away EDFGSFG
3 CL CORP Traded PDFEFGR
细节:
print(dict(Test_Data)
{'originating_system_id': ['CL', 'CL', 'CL', 'CL'],
'rbc_security_type1': ['CORP', 'CORP', 'CORP', 'CORP'],
'state': ['Traded', 'Covered', 'Traded Away', 'Traded'],
'trading_book': ['LCAAAAA', 'NUBBBBB', 'EDFGSFG', 'PDFEFGR']








暂无数据