将多个字典添加到Pandas数据帧中:错误DataFrame构造函数未正确调用?
将多个字典添加到Pandas数据帧中:错误DataFrame构造函数未正确调用?
我正在尝试将一个 `dictionary` 转换为 `pandas` 的 `dataframe`。但是我遇到了一些问题。我尝试了下面的方法:
data = {'applicableMargin': '12.50', 'marketType': 'N', 'totalBuyQuantity': '1,14,514', 'buyPrice1': '1,546.30', 'dayLow': '1,541.20', 'symbol': 'ACC', 'cm_adj_low_dt': '23-MAR-17', 'open': '1,571.50', 'sellPrice2': '1,547.85', 'sellPrice4': '1,547.95', 'cm_ffm': '13,249.84', 'buyPrice3': '1,546.00', 'css_status_desc': 'Listed', 'ndStartDate': '-', 'buyQuantity1': '43', 'totalTradedValue': '1,468.42', 'surv_indicator': '-', 'recordDate': '26-JUL-17', 'secDate': '16MAR2018', 'faceValue': '10.00', 'totalTradedVolume': '94,384', 'pricebandlower': '1,411.20', 'sellQuantity4': '16', 'averagePrice': '1,555.79', 'buyPrice2': '1,546.05', 'totalSellQuantity': '84,873', 'closePrice': '0.00', 'buyPrice4': '1,545.90', 'extremeLossMargin': '5.00', 'isinCode': 'INE012A01025', 'buyQuantity4': '48', 'sellPrice3': '1,547.90', 'bcEndDate': '-', 'buyQuantity5': '27', 'indexVar': '-', 'purpose': 'INTERIM DIVIDEND - RS 11/- PER SHARE', 'sellQuantity5': '286', 'series': 'EQ', 'low52': '1,380.40', 'dayHigh': '1,573.70', 'pricebandupper': '1,724.70', 'basePrice': '1,567.95', 'lastPrice': '1,546.05', 'sellQuantity2': '32', 'deliveryToTradedQuantity': '50.45', 'high52': '1,869.95', 'cm_adj_high_dt': '13-SEP-17', 'sellQuantity1': '67', 'buyQuantity2': '155', 'isExDateFlag': False, 'quantityTraded': '2,53,481', 'previousClose': '1,567.95', 'securityVar': '5.74', 'bcStartDate': '-', 'sellQuantity3': '25', 'ndEndDate': '-', 'buyQuantity3': '31', 'companyName': 'ACC Limited', 'sellPrice1': '1,547.65', 'adhocMargin': '-', 'sellPrice5': '1,548.00', 'change': '-21.90', 'exDate': '25-JUL-17', 'varMargin': '7.50', 'pChange': '-1.40', 'buyPrice5': '1,545.85', 'priceBand': 'No Band'} pd_cols = [] for i in data: pd_cols.append(i) #fut_data = pd.DataFrame() #fut_data.columns = pd_cols fut_data = pd.DataFrame(data.items(), columns=pd_cols)
这个方法报错了:
Traceback (most recent call last):
File \"\", line 1, in
File \"C:\\Python34\\lib\\site-packages\\pandas\\core\\frame.py\", line 345, in >init
raise PandasError(\'DataFrame constructor not properly called!\')
pandas.core.common.PandasError: DataFrame constructor not properly called!
之后我会拥有更多具有相同列的 `dict`,我想将它们全部添加到同一个 `database` 中。
先行谢过。
admin 更改状态以发布 2023年5月21日
这个是否给你想要的输出?
import pandas as pd data = {'applicableMargin': '12.50', 'marketType': 'N', 'totalBuyQuantity': '1,14,514', 'buyPrice1': '1,546.30', 'dayLow': '1,541.20', 'symbol': 'ACC', 'cm_adj_low_dt': '23-MAR-17', 'open': '1,571.50', 'sellPrice2': '1,547.85', 'sellPrice4': '1,547.95', 'cm_ffm': '13,249.84', 'buyPrice3': '1,546.00', 'css_status_desc': 'Listed', 'ndStartDate': '-', 'buyQuantity1': '43', 'totalTradedValue': '1,468.42', 'surv_indicator': '-', 'recordDate': '26-JUL-17', 'secDate': '16MAR2018', 'faceValue': '10.00', 'totalTradedVolume': '94,384', 'pricebandlower': '1,411.20', 'sellQuantity4': '16', 'averagePrice': '1,555.79', 'buyPrice2': '1,546.05', 'totalSellQuantity': '84,873', 'closePrice': '0.00', 'buyPrice4': '1,545.90', 'extremeLossMargin': '5.00', 'isinCode': 'INE012A01025', 'buyQuantity4': '48', 'sellPrice3': '1,547.90', 'bcEndDate': '-', 'buyQuantity5': '27', 'indexVar': '-', 'purpose': 'INTERIM DIVIDEND - RS 11/- PER SHARE', 'sellQuantity5': '286', 'series': 'EQ', 'low52': '1,380.40', 'dayHigh': '1,573.70', 'pricebandupper': '1,724.70', 'basePrice': '1,567.95', 'lastPrice': '1,546.05', 'sellQuantity2': '32', 'deliveryToTradedQuantity': '50.45', 'high52': '1,869.95', 'cm_adj_high_dt': '13-SEP-17', 'sellQuantity1': '67', 'buyQuantity2': '155', 'isExDateFlag': False, 'quantityTraded': '2,53,481', 'previousClose': '1,567.95', 'securityVar': '5.74', 'bcStartDate': '-', 'sellQuantity3': '25', 'ndEndDate': '-', 'buyQuantity3': '31', 'companyName': 'ACC Limited', 'sellPrice1': '1,547.65', 'adhocMargin': '-', 'sellPrice5': '1,548.00', 'change': '-21.90', 'exDate': '25-JUL-17', 'varMargin': '7.50', 'pChange': '-1.40', 'buyPrice5': '1,545.85', 'priceBand': 'No Band'} df = pd.DataFrame.from_dict([data]) print(df.iloc[:,:5])
当我运行上面的代码时,我得到一个1行的数据框:
adhocMargin applicableMargin averagePrice basePrice bcEndDate 0 - 12.50 1,555.79 1,567.95 -
如果你有多个类似的字典,可以像这样将它们全部放入一个列表中:
df = pd.DataFrame.from_dict([data1,data2])
这将导致每个字典对应一行的数据框。
这对我来说有效。由于这在您这里出错了,可能是您复制粘贴出现了错误。
fut_data = pd.DataFrame.from_dict(data, orient='index').T print(fut_data) # applicableMargin marketType totalBuyQuantity buyPrice1 dayLow symbol \ # 0 12.50 N 1,14,514 1,546.30 1,541.20 ACC # cm_adj_low_dt open sellPrice2 sellPrice4 ... companyName \ # 0 23-MAR-17 1,571.50 1,547.85 1,547.95 ... ACC Limited # buyPrice5 priceBand # 0 1,545.85 No Band # [1 rows x 67 columns]
您可以按以下方式附加:
df = pd.DataFrame.from_dict(data, orient='index').T df = df.append(pd.DataFrame.from_dict(data2, orient='index').T)
这里的data2
是另一个类似的字典。