简体   繁体   中英

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

{
    "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

        "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: 看图片

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 :

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)


```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM