簡體   English   中英

如何在python中連接熊貓數據框

[英]How to concat pandas dataframe in python

我有一個python對象列表,有些像這樣

team_1_players1=[] is a list

在team_1_players1 = []內部,存儲了多個json對象。

1st Json Object像這樣

[[{'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 Object就是這樣

[[{'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])

我有1000多個json對象,它們存儲在team_1_players1列表中,所以有什么有效的方法

首先,展平嵌套列表:

import itertools as it 

data = list(it.chain.from_iterable(team_1_players1))

然后使用pd.DataFrame.from_records(data)

df = pd.DataFrame.from_records(data)

您可以嘗試以下代碼。 我假設team1的所有json對象都為a ,第二個jsonb

a = [[{'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'}]]
b = [[{'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'}]]

if len (a)==1 and len(b) ==1: # this checks if objects are single entity of players or multiple teams.

    team_1 = pd.DataFrame.from_dict(a[0])
    team_2 = pd.DataFrame.from_dict(b[0])
    merged_team = pd.concat([df,df1], ignore_index=True)

merged_team

您將得到如下輸出:

在此處輸入圖片說明

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM