我希望通过建议的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
当我在df分配上更改为from_dict
时:
df = pd.DataFrame.from_dict(Test_Data)
当我希望应用过滤器时出现以下行错误:
m1 = ~df['trading_book'].str.startswith(tuple(prefixes))
KeyError: 'trading_book'
from_dict
的结构不同吗?有from_items
以外的替代方法吗?
答案 0 :(得分:1)
对我来说工作愉快,将其转换为字典:
.py
详细信息:
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