[英]Python dictionary from API has 1 heading I want to get rid of
so this is what i get from the API, but I really dont need the first "dividends" heading所以这就是我从 API 得到的,但我真的不需要第一个“红利”标题
{
"dividends": [
{
"currency": "USD",
"date": "2021-05-04",
"dividend": "0.1100",
"dividend_prior": "",
"dividend_type": "Cash",
"dividend_yield": "0.0878828229027963",
"ex_dividend_date": "2021-05-18",
"exchange": "AMEX",
"frequency": 12,
"id": "6091befc99cf5000019edaf3",
"importance": 0,
"name": "Pioneer Diversified High",
"notes": "",
"payable_date": "2021-05-28",
"record_date": "2021-05-19",
"ticker": "HNW",
"updated": 1620164698
},
{
"currency": "USD",
"date": "2021-05-04",
"dividend": "0.0475",
"dividend_prior": "",
"dividend_type": "Cash",
"dividend_yield": "0.0449526813880126",
"ex_dividend_date": "2021-05-18",
"exchange": "NYSE",
"frequency": 12,
"id": "6091bf0999cf5000019edaff",
"importance": 0,
"name": "Pioneer Municipal High IT",
"notes": "",
"payable_date": "2021-05-28",
"record_date": "2021-05-19",
"ticker": "MHI",
"updated": 1620164711
},
I got it working with header=False, but when put into a csv file it doesnt take the correct new headers from this我让它与 header=False 一起工作,但是当放入 csv 文件时,它不会从中获取正确的新标题
"currency": "USD",
"date": "2021-05-04",
"dividend": "0.1100",
"dividend_prior": "",
"dividend_type": "Cash",
"dividend_yield": "0.0878828229027963",
"ex_dividend_date": "2021-05-18",
"exchange": "AMEX",
"frequency": 12,
"id": "6091befc99cf5000019edaf3",
"importance": 0,
"name": "Pioneer Diversified High",
"notes": "",
"payable_date": "2021-05-28",
"record_date": "2021-05-19",
"ticker": "HNW",
"updated": 1620164698
The output of that looks like this: output 看起来像这样:
shouldnt it take the headers and put them as the column names and then align everything according to columns?不应该将标题作为列名,然后根据列对齐所有内容吗?
this is the code that ive used so far:这是我到目前为止使用的代码:
response_dumped = json.dumps(response)
response_parsed = json.loads(response_dumped)
df = pd.DataFrame(response_parsed)
df.to_csv("main_data.csv", index=False, header=False)
how would I achieve that?我将如何实现这一目标?
You are close, select data by key dividends
:你很接近,select 数据按主要
dividends
:
df= pd.DataFrame(esponse_parsed['dividends'])
import pandas as pd
json = {
"dividends": [
{
"currency": "USD",
"date": "2021-05-04",
"dividend": "0.1100",
"dividend_prior": "",
"dividend_type": "Cash",
"dividend_yield": "0.0878828229027963",
"ex_dividend_date": "2021-05-18",
"exchange": "AMEX",
"frequency": 12,
"id": "6091befc99cf5000019edaf3",
"importance": 0,
"name": "Pioneer Diversified High",
"notes": "",
"payable_date": "2021-05-28",
"record_date": "2021-05-19",
"ticker": "HNW",
"updated": 1620164698
},
{
"currency": "USD",
"date": "2021-05-04",
"dividend": "0.0475",
"dividend_prior": "",
"dividend_type": "Cash",
"dividend_yield": "0.0449526813880126",
"ex_dividend_date": "2021-05-18",
"exchange": "NYSE",
"frequency": 12,
"id": "6091bf0999cf5000019edaff",
"importance": 0,
"name": "Pioneer Municipal High IT",
"notes": "",
"payable_date": "2021-05-28",
"record_date": "2021-05-19",
"ticker": "MHI",
"updated": 1620164711
}]}
no_header = json["dividends"]
df = pd.DataFrame(no_header)
df.to_csv("main_data.csv", index=False, header=False)
```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.