我有一个python对象列表
team_1_players1=[] is a list
在team_1_players1 = []内部,存储了多个json对象。
第一个这样的Json对象
[[{'age_days': '72', 'age_years': '30', 'alpha_name': 'RAVAL,JA', 'batting_hand': 'left-hand batsman', 'batting_style': 'lhb', 'batting_style_long': 'left-hand bat', 'bowling_hand': 'right-arm bowler', 'bowling_pacespin': 'spin bowler', 'bowling_style': 'lb', 'bowling_style_long': 'legbreak ', 'captain': '0', 'card_long': 'JA Raval', 'card_qualifier': '', 'card_short': 'Raval', 'dob': '1988-09-22', 'keeper': '0', 'known_as': 'Jeet Raval', 'mobile_name': 'Raval', 'object_id': '277914', 'player_id': '51094', 'player_primary_role': 'opening batsman', 'player_style_id': '1', 'player_type': '1', 'player_type_name': 'player', 'popular_name': 'Raval', 'portrait_alt_id': '1', 'portrait_object_id': '397349', 'status_id': '3'}, {'age_days': '245', 'age_years': '26', 'alpha_name': 'LATHAM,TWM', 'batting_hand': 'left-hand batsman', 'batting_style': 'lhb', 'batting_style_long': 'left-hand bat', 'bowling_hand': 'right-arm bowler', 'bowling_pacespin': 'pace bowler', 'bowling_style': 'rm', 'bowling_style_long': 'right-arm medium ', 'captain': '0', 'card_long': 'TWM Latham', 'card_qualifier': '', 'card_short': 'Latham', 'dob': '1992-04-02', 'keeper': '0', 'known_as': 'Tom Latham', 'mobile_name': 'Latham', 'object_id': '388802', 'player_id': '59148', 'player_primary_role': 'wicketkeeper batsman', 'player_style_id': '7', 'player_type': '1', 'player_type_name': 'player', 'popular_name': 'Latham', 'portrait_alt_id': '1', 'portrait_object_id': '1099471', 'status_id': '3'}]]
当我试图存储在数据框中时,我会变成这样
0{'age_days': '72', 'age_years': '30', 'alpha_n...}
1 {'age_days': '196', 'age_years': '23', 'alpha_...}
2 {'age_days': '99', 'age_years': '29', 'alpha_n...}
第二个Json对象就是这样
[[{'age_days': '123', 'age_years': '25', 'alpha_name': 'LIVINGSTONE,LS', 'batting_hand': 'right-hand batsman', 'batting_style': 'rhb', 'batting_style_long': 'right-hand bat', 'bowling_hand': 'right-arm bowler', 'bowling_pacespin': 'spin bowler', 'bowling_style': 'lb', 'bowling_style_long': 'legbreak ', 'captain': '0', 'card_long': 'LS Livingstone', 'card_qualifier': '', 'card_short': 'Livingstone', 'dob': '1993-08-04', 'keeper': '0', 'known_as': 'Liam Livingstone', 'mobile_name': '', 'object_id': '403902', 'player_id': '59832', 'player_primary_role': None, 'player_style_id': None, 'player_type': '1', 'player_type_name': 'player', 'popular_name': '', 'portrait_alt_id': '1', 'portrait_object_id': '863825', 'status_id': '3'}, {'age_days': '142', 'age_years': '23', 'alpha_name': 'HAIN,SR', 'batting_hand': 'right-hand batsman', 'batting_style': 'rhb', 'batting_style_long': 'right-hand bat', 'bowling_hand': 'right-arm bowler', 'bowling_pacespin': 'spin bowler', 'bowling_style': 'ob', 'bowling_style_long': 'right-arm offbreak ', 'captain': '0', 'card_long': 'SR Hain', 'card_qualifier': '', 'card_short': 'Hain', 'dob': '1995-07-16', 'keeper': '0', 'known_as': 'Sam Hain', 'mobile_name': 'Hain', 'object_id': '555850', 'player_id': '67482', 'player_primary_role': None, 'player_style_id': None, 'player_type': '1', 'player_type_name': 'player', 'popular_name': 'Hain', 'portrait_alt_id': '1', 'portrait_object_id': '631902', 'status_id': '3'}]]
我想将所有json对象存储到数据框中 这是将json对象添加到列表中的代码
team_1_players1=[]
for x in matchList:
m=MyMatch(x)
team_1_players1.append(m.team_1_players)
这就是我将team_1_players1列表转换为数据框的方式
for i in range(team_1_players1):
df = pd.DataFrame(team_1_players1[i])
当我仅使用此行而没有循环时
df = pd.DataFrame(team_1_players1[0])
我正在得到这样的数据框
当我像这样打印第二个索引时,我会得到结果
df = pd.DataFrame(team_1_players1[1])
现在我要合并所有结果
这就是为什么我使用for循环遍历所有数据框的原因
for i in range(team_1_players1):
df = pd.DataFrame(team_1_players1[i])
我在team_1_players1列表中存储了1000多个json对象,所以有什么有效的方法
答案 0 :(得分:0)
首先,展平嵌套列表:
import itertools as it
data = list(it.chain.from_iterable(team_1_players1))
然后使用pd.DataFrame.from_records(data)
:
df = pd.DataFrame.from_records(data)
答案 1 :(得分:0)
您可以尝试以下代码。我假设您为team1使用的所有 with cte as
(
SELECT
farm_products.id as ID,
farm_products.product as Product1,
shop_products.product as Product2,
row_number() over(partition by farm_products.id order by
farm_products.product) rn
FROM
farm_products join
shop_products on farm_products.product != shop_products.product
join shop_farm on shop_farm.farm_id = shop_products.id
and farm_products.id = shop_farm.farm_id
) select * from cte where rn=1
对象为json
,第二个a
为json
b
您将获得如下输出:-
希望这会有所帮助。