简体   繁体   English

Python 字典来自 API 有 1 个标题我想摆脱

[英]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.

相关问题 我想摆脱具有特定日期的 dataframe 的一部分 - I want to get rid of part of a dataframe that has a certain date 我想使用 selenium python 从 API 响应中获取 orderID - I want to get orderID from API response using selenium python 如何在 Python 中使用 Pandas 库摆脱系列标题(列标题) - How to get rid of Series heading (column heading) using Pandas Library in Python 摆脱NaN作为python字典中的键 - Get rid of NaN as key in python dictionary 字典有一个单独的字典,我想在 python 中的 dataframe 中转换它,以便该表包含具有子列的列 - A dictionary has a separate dictionary and i want to convert it in dataframe in python such that the table contains columns which has sub columns 如何摆脱python上的“程序仍在运行,您确定要杀死它”警告? - How do I get rid of the ' the program is still running are you sure you want to kill it' warning on python? 我想在 function - Python 中使用字典中的特定值 - I want to use a specific value from a dictionary in a function - Python 我想从字典中获取两个最小的数字 - I want to get the two smallest numbers from a dictionary 我想以复杂的方式从数据列表中获取字典列表? - I want to get a list of dictionary from a list of data in a complex way? 如何从我想成为整数的字符串列表中摆脱“ K” - How do I get rid of “K” from a list of string which I want to be integer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM