我正在尝试将数据添加到熊猫数据框。我有问题进入子目录。理想情况下,我希望第一列中的所有比率名称都像“ priceBookValueRatio”,“ priceToBookRatio”,... ect,第一行中的日期从左到右,每个日期下的适当值。有人可以帮忙吗?
这是我的代码
def get_jsonparsed_data(ticker):
url = ("https://financialmodelingprep.com/api/v3/financial-ratios/" + ticker)
response = urlopen(url)
data = response.read().decode("utf-8")
value = json.loads(data)
data = value["ratios"]
df = pd.DataFrame(data)
print df
这是我的数据
{
"symbol" : "AAPL",
"ratios" : [ {
"date" : "2019-09-28",
"investmentValuationRatios" : {
"priceBookValueRatio" : "11.1154",
"priceToBookRatio" : "11.1154",
"priceToSalesRatio" : "3.8903",
"priceEarningsRatio" : "18.7109",
"receivablesTurnover" : "5.489",
"priceToFreeCashFlowsRatio" : "17.5607",
"priceToOperatingCashFlowsRatio" : "14.5863",
"priceCashFlowRatio" : "0",
"priceEarningsToGrowthRatio" : "0",
"priceSalesRatio" : "0",
"dividendYield" : "",
"enterpriseValueMultiple" : "1.7966762045884",
"priceFairValue" : "0"
},
"profitabilityIndicatorRatios" : {
"niperEBT" : "0.84056163195765",
"ebtperEBIT" : "1",
"ebitperRevenue" : "0.25266552384174",
"grossProfitMargin" : "0.37817768109035",
"operatingProfitMargin" : "1",
"pretaxProfitMargin" : "0.24572017188497",
"netProfitMargin" : "0.21238094505984",
"effectiveTaxRate" : "0.15943836804235",
"returnOnAssets" : "0.5848",
"returnOnEquity" : "0.6106",
"returnOnCapitalEmployed" : "0.2691",
"nIperEBT" : "0.84056163195765",
"eBTperEBIT" : "1",
"eBITperRevenue" : "0.25266552384174"
},
"operatingPerformanceRatios" : {
"receivablesTurnover" : "5.489",
"payablesTurnover" : "1.2542",
"inventoryTurnover" : "64.5433",
"fixedAssetTurnover" : "6.9606185456686",
"assetTurnover" : "0.76857223883066"
},
"liquidityMeasurementRatios" : {
"currentRatio" : "1.54",
"quickRatio" : "1.3844473032029",
"cashRatio" : "0.46202160464632",
"daysOfSalesOutstanding" : "-9.2636",
"daysOfInventoryOutstanding" : "64.2588",
"operatingCycle" : "",
"daysOfPayablesOutstanding" : "64.8648",
"cashConversionCycle" : ""
},
"debtRatios" : {
"debtRatio" : "0.3192",
"debtEquityRatio" : "1.194",
"longtermDebtToCapitalization" : "0.50361776241806",
"totalDebtToCapitalization" : "0.54422142191553",
"interestCoverage" : "0.0",
"cashFlowToDebtRatio" : "0.64222977037771",
"companyEquityMultiplier" : "3.7410043320661"
},
"cashFlowIndicatorRatios" : {
"operatingCashFlowPerShare" : "15.0267",
"freeCashFlowPerShare" : "12.94",
"cashPerShare" : "10.5773",
"payoutRatio" : "0.251",
"receivablesTurnover" : "5.489",
"operatingCashFlowSalesRatio" : "0.26670997101939",
"freeCashFlowOperatingCashFlowRatio" : "0.84875560231154",
"cashFlowCoverageRatios" : "0.64222977037771",
"shortTermCoverageRatios" : "4.2728448275862",
"capitalExpenditureCoverageRatios" : "6.6118151500715",
"dividendpaidAndCapexCoverageRatios" : "2.8191679531974",
"dividendPayoutRatio" : "0.25551976255972"
}
},
{
"date" : "2018-09-29",
"investmentValuationRatios" : {
"priceBookValueRatio" : "10.1842",
"priceToBookRatio" : "10.1842",
"priceToSalesRatio" : "4.1328",
"priceEarningsRatio" : "18.9226",
"receivablesTurnover" : "6.2738",
"priceToFreeCashFlowsRatio" : "17.563",
"priceToOperatingCashFlowsRatio" : "14.1753",
"priceCashFlowRatio" : "14.375642493446",
"priceEarningsToGrowthRatio" : "18.698887988401",
"priceSalesRatio" : "4.1912065394209",
"dividendYield" : "0.012318046710734",
"enterpriseValueMultiple" : "14.710301181747",
"priceFairValue" : "10.389124295011"
},
"profitabilityIndicatorRatios" : {
"niperEBT" : "0.81657819294131",
"ebtperEBIT" : "1",
"ebitperRevenue" : "0.27448935409176",
"grossProfitMargin" : "0.38343718820008",
"operatingProfitMargin" : "1",
"pretaxProfitMargin" : "0.26694026619477",
"netProfitMargin" : "0.22414202074587",
"effectiveTaxRate" : "0.18342180705869",
"returnOnAssets" : "1.0497",
"returnOnEquity" : "0.5556",
"returnOnCapitalEmployed" : "0.217",
"nIperEBT" : "0.81657819294131",
"eBTperEBIT" : "1",
"eBITperRevenue" : "0.27448935409176"
},
"operatingPerformanceRatios" : {
"receivablesTurnover" : "6.2738",
"payablesTurnover" : "1.2564",
"inventoryTurnover" : "60.2871",
"fixedAssetTurnover" : "6.4302488863064",
"assetTurnover" : "0.72621505229339"
},
"liquidityMeasurementRatios" : {
"currentRatio" : "1.133",
"quickRatio" : "0.99453976140569",
"cashRatio" : "0.22352474359306",
"daysOfSalesOutstanding" : "-8.8176",
"daysOfInventoryOutstanding" : "67.3325",
"operatingCycle" : "",
"daysOfPayablesOutstanding" : "76.8054",
"cashConversionCycle" : ""
},
"debtRatios" : {
"debtRatio" : "0.313",
"debtEquityRatio" : "1.0685",
"longtermDebtToCapitalization" : "0.46661721806832",
"totalDebtToCapitalization" : "0.51655010603258",
"interestCoverage" : "0.0",
"cashFlowToDebtRatio" : "0.67637989919901",
"companyEquityMultiplier" : "3.4133013523477"
},
"cashFlowIndicatorRatios" : {
"operatingCashFlowPerShare" : "15.6263",
"freeCashFlowPerShare" : "9.924",
"cashPerShare" : "5.2293",
"payoutRatio" : "0.226",
"receivablesTurnover" : "6.2738",
"operatingCashFlowSalesRatio" : "0.29154916319961",
"freeCashFlowOperatingCashFlowRatio" : "0.8280729395356",
"cashFlowCoverageRatios" : "0.67637989919901",
"shortTermCoverageRatios" : "3.7321187584345",
"capitalExpenditureCoverageRatios" : "5.8164200405619",
"dividendpaidAndCapexCoverageRatios" : "2.8652728954672",
"dividendPayoutRatio" : "0.2303337756799"
}
},
{
"date" : "2017-09-30",
"investmentValuationRatios" : {
"priceBookValueRatio" : "5.9086",
"priceToBookRatio" : "5.9086",
"priceToSalesRatio" : "3.4657",
"priceEarningsRatio" : "16.5922",
"receivablesTurnover" : "7.0564",
"priceToFreeCashFlowsRatio" : "15.4994",
"priceToOperatingCashFlowsRatio" : "12.37",
"priceCashFlowRatio" : "12.166429629599",
"priceEarningsToGrowthRatio" : "16.160760748713",
"priceSalesRatio" : "3.4086956688842",
"dividendYield" : "0.016341413728755",
"enterpriseValueMultiple" : "12.106846738693",
"priceFairValue" : "5.8292161925369"
},
"profitabilityIndicatorRatios" : {
"niperEBT" : "0.75443523849647",
"ebtperEBIT" : "1",
"ebitperRevenue" : "0.27957894553164",
"grossProfitMargin" : "0.38469860491899",
"operatingProfitMargin" : "1",
"pretaxProfitMargin" : "0.2676042820873",
"netProfitMargin" : "0.21092420845075",
"effectiveTaxRate" : "0.24556476150353",
"returnOnAssets" : "0.7847",
"returnOnEquity" : "0.3607",
"returnOnCapitalEmployed" : "0.1752",
"nIperEBT" : "0.75443523849647",
"eBTperEBIT" : "1",
"eBITperRevenue" : "0.27957894553164"
},
"operatingPerformanceRatios" : {
"receivablesTurnover" : "7.0564",
"payablesTurnover" : "1.2897",
"inventoryTurnover" : "65.6173",
"fixedAssetTurnover" : "6.7854838232247",
"assetTurnover" : "0.61077110404749"
},
"liquidityMeasurementRatios" : {
"currentRatio" : "1.276",
"quickRatio" : "1.089670085504",
"cashRatio" : "0.20125181026445",
"daysOfSalesOutstanding" : "-12.5636",
"daysOfInventoryOutstanding" : "56.8007",
"operatingCycle" : "",
"daysOfPayablesOutstanding" : "70.4447",
"cashConversionCycle" : ""
},
"debtRatios" : {
"debtRatio" : "0.3082",
"debtEquityRatio" : "0.863",
"longtermDebtToCapitalization" : "0.42034732372197",
"totalDebtToCapitalization" : "0.46322584262014",
"interestCoverage" : "0.0",
"cashFlowToDebtRatio" : "0.55519536652835",
"companyEquityMultiplier" : "2.7999060031183"
},
"cashFlowIndicatorRatios" : {
"operatingCashFlowPerShare" : "12.3101",
"freeCashFlowPerShare" : "9.779",
"cashPerShare" : "3.8888",
"payoutRatio" : "0.259",
"receivablesTurnover" : "7.0564",
"operatingCashFlowSalesRatio" : "0.28017222576058",
"freeCashFlowOperatingCashFlowRatio" : "0.80613468275594",
"cashFlowCoverageRatios" : "0.55519536652835",
"shortTermCoverageRatios" : "3.476695718075",
"capitalExpenditureCoverageRatios" : "5.1582202232752",
"dividendpaidAndCapexCoverageRatios" : "2.5465900079302",
"dividendPayoutRatio" : "0.26408967756613"
}
},
答案 0 :(得分:0)
pandas
无法从任意嵌套的字典构造DataFrame
。您需要以可以解析的标准格式传递数据。
一种方法是创建包含键和值的单级字典列表,然后从中构造DataFrame
def clean_data(d):
ret = {}
ret['date'] = d['date']
for outer_key, rec in d.items():
if outer_key != 'date':
for k,v in rec.items():
ret[k]= v
return ret
cleaned_data = [clean_data(d) for d in data['ratios']]
df = pd.DataFrame.from_records(cleaned_data, index='date')
df = df.transpose()
会给你