我正在尝试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)
这会出错:
追踪(最近一次通话): 文件"",第1行,in 文件" C:\ Python34 \ lib \ site-packages \ pandas \ core \ frame.py",第345行,> init 引发PandasError(' DataFrame构造函数未正确调用!') pandas.core.common.PandasError:未正确调用DataFrame构造函数!
在此之后,我会有更多dict
,它们将具有相同的columns
。我想将它们全部添加到同一个database
。
提前致谢。
答案 0 :(得分:1)
这能为您提供所需的输出吗?
<android.support.design.widget.AppBarLayout
android:id="@+id/htab_appbar"
android:layout_width="match_parent"
android:layout_height="250dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/appBarImg"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@drawable/ic_launcher_background"
app:layout_collapseMode="parallax" />
</LinearLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#FFF"
app:tabMode="scrollable"
app:tabPaddingEnd="15dp"
app:tabPaddingStart="15dp">
<android.support.design.widget.TabItem
android:id="@+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Schengen" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rest Of World" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Worldwide" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
当我运行上面的内容时,我得到一个1行的数据帧:
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])
如果您有多个类似的词组,请将它们全部放在如下列表中:
adhocMargin applicableMargin averagePrice basePrice bcEndDate
0 - 12.50 1,555.79 1,567.95 -
这导致每个字典有一行的数据帧。
答案 1 :(得分:1)
这对我有用。由于此错误,您可能会出现复制粘贴错误。
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
是另一个类似词典。
答案 2 :(得分:0)
虽然以前的答案更好,但如果它适合你,你可以试试这个荒谬的解决方案:
fut_data = pd.DataFrame(data,index=[0])
要添加更多行,您可以尝试:
fut_data1 = pd.DataFrame(data1,index=[1])
fut_data.append(fut_data1)
或
fut_data1 = pd.DataFrame(data1,index=[i]) #where i is a loop variable
fut_data.append(fut_data1)