我試圖修改整列值,但我一直有列表超出範圍的問題。這是我的全部代碼:應用函數導致列表索引超出範圍
# Libraries
import json, requests
import pandas as pd
from pandas.io.json import json_normalize
# Set URL
url = 'https://api-v2.themuse.com/jobs'
# For loop to extract data
for i in range(100):
data = json.loads(requests.get(
url=url,
params={'page': i}
).text)['results']
# JSON to PANDAS
data_norm = pd.read_json(json.dumps(data))
# Modify two columns' values
data_norm.locations = data_norm.locations.apply(lambda x: [{x[0]['name']}])
data_norm.publication_date = pd.to_datetime(data_norm.publication_date)
這裏的問題是,當我使用的功能
data_norm.locations = data_norm.locations.apply(lambda x: [{x[0]['name']}])
我收到以下錯誤:
IndexError: list index out of range
理想情況下,我想改變location
從此列:
0 [{'name': 'Seattle, WA'}]
1 [{'name': 'San Francisco Bay Area'}]
2 [{'name': 'Palo Alto, CA'}]
3 [{'name': 'Reno, NV'}]
4 []
Name: locations, dtype: object
成這樣:
0 Seattle, WA
1 San Francisco Bay Area
2 Palo Alto, CA
3 Reno, NV
4
Name: locations, dtype: object
顯示data_norm.head(),以獲得更好的幫助。然而,這聽起來data_norm索引是不是整數,或者至少沒有0在它至少 – Boud
@Boud剛剛更新了原來的帖子 –