从 json 中提取日期和数字并将它们附加到 pandas 数据框
Posted
技术标签:
【中文标题】从 json 中提取日期和数字并将它们附加到 pandas 数据框【英文标题】:Pull date and numbers from json and append them to pandas dataframe 【发布时间】:2022-01-13 23:06:04 【问题描述】:我想提取日期和强力球号码并将它们附加到熊猫数据框。我已经制作了列,但我似乎无法将数据获取到列中。当我转到https://jsonparser.org/ 并输入网址时,我看到了。但是当我尝试列出数字 I.E. ['8'] 或 ['9'] 它不附加数据。我已经为此工作了大约 3 天。提前致谢。
###########
# MODULES #
###########
import json
import requests
import urllib
import pandas as pd
###########
# HISTORY #
###########
#We need to pull the data from the website.
#Then we need to organize the numbers based off of position.
#Basically it will be several lists of numbers
URL = "https://data.ny.gov/api/views/d6yy-54nr/rows"
r = requests.get(URL)
my_json = r.json()
#my_json_dumps = json.dumps(my_json, indent = 2)
#print(my_json_dumps)
df = pd.DataFrame(columns=["Date","Powerball Numbers","1","2","3","4","5","6","7"])#Create columns in df
for item in my_json['data']:
df = pd.DataFrame(my_json['data'])
l_date = df.iloc['8']#Trying to pull columns from json
p_num = (df.iloc['9'])#Trying to pull columns from json
df = df.append("Date": l_date,
"Powerball Numbers": p_num,
,ignore_index=True)
#test = item['id']
print(l_date)
编辑:这就是我想要得到的。
【问题讨论】:
【参考方案1】:试试这个:
旧代码:
l_date = df.iloc['8'] #Trying to pull columns from json
p_num = (df.iloc['9']) #Trying to pull columns from json
用引号改变这一行 df.iloc['8'] :
l_date = df.iloc[8] # without quotation
p_num = (df.iloc[9]) # without quotation
工作正常。结果是:
0 row-w3y7.4r9a-caat
1 00000000-0000-0000-3711-B495A26E6E8B
2 0
3 1619710755
4 None
5 1619710755
6 None
7
8 2020-10-24T00:00:00
9 18 20 27 45 65 06
10 2
Name: 8, dtype: object
0 row-w3y7.4r9a-caat
1 00000000-0000-0000-3711-B495A26E6E8B
2 0
3 1619710755
4 None
5 1619710755
6 None
7
8 2020-10-24T00:00:00
9 18 20 27 45 65 06
10 2
希望对你有用。
【讨论】:
我想我们越来越近了。我想将日期和数字附加到数据框中。所以对于我想要日期的列,强力球号码(全部),1、2、3、4、5、6(每个数字分开)。当我查看我的数据框时,它仍然显示['row-nx2r_usun.g52b', '00000000-0000-0000-9D99-B9EA775FDC5E', 0, 1619710755, None, 1619710755, None, ' ', '2020-09-26T00:00:00', '11 21 27 36 62 24', '3']
【参考方案2】:
我就是这样做的:
URL = "https://data.ny.gov/resource/d6yy-54nr.json"
r = requests.get(URL)
my_json = r.json()
for item in my_json:
df = pd.DataFrame(my_json)
l_date = item['draw_date']#Trying to pull columns from json
p_num = item['winning_numbers']#Trying to pull columns from json
n_list = list(map(int, p_num.split())) #Convert powerball numbers into list
n_1 = n_list[0]
n_2 = n_list[1]
n_3 = n_list[2]
n_4 = n_list[3]
n_5 = n_list[4]
n_6 = n_list[5]
df1 = pd.DataFrame(columns=["Date","Powerball Numbers", "1","2","3","4","5","6"])#Create columns in df
df = df1.append("Date": l_date,
"Powerball Numbers": p_num,
"1": n_1,
"2": n_2,
"3": n_3,
"4": n_4,
"5": n_5,
"6": n_6,
,ignore_index=True)
print(df)
生产的
【讨论】:
以上是关于从 json 中提取日期和数字并将它们附加到 pandas 数据框的主要内容,如果未能解决你的问题,请参考以下文章
如何按 > 日期对一系列日期求和并将它们附加到熊猫新数据框中的新列?