I have these data taken from soccer stat site:
[{'id': '18209', 'isResult': True, 'side': 'h', 'h': {'id': '89', 'title': 'Manchester United', 'short_title': 'MUN'}, 'a': {'id': '220', 'title': 'Brighton', 'short_title': 'BRI'}, 'goals': {'h': '1', 'a': '2'}, 'xG': {'h': '1.42103', 'a': '1.7289'}, 'datetime': '2022-08-07 13:00:00'}, {'id': '18218', 'isResult': True, 'side': 'a', 'h': {'id': '244', 'title': 'Brentford', 'short_title': 'BRE'}, 'a': {'id': '89', 'title': 'Manchester United', 'short_title': 'MUN'}, 'goals': {'h': '4', 'a': '0'}, 'xG': {'h': '1.38785', 'a': '0.896038'}, 'datetime': '2022-08-13 16:30:00'}, {'id': '18231', 'isResult': True, 'side': 'h', 'h': {'id': '89', 'title': 'Manchester United', 'short_title': 'MUN'}, 'a': {'id': '87', 'title': 'Liverpool', 'short_title': 'LIV'}, 'goals': {'h': '2', 'a': '1'}, 'xG': {'h': '2.01764', 'a': '1.52301'}, 'datetime': '2022-08-22 19:00:00'}, {'id': '18232', 'isResult': True, 'side': 'a', 'h': {'id': '74', 'title': 'Southampton', 'short_title': 'SOU'}, 'a': {'id': '89', 'title': 'Manchester United', 'short_title': 'MUN'}, 'goals': {'h': '0', 'a': '1'}, 'xG': {'h': '1.35887', 'a': '1.34359'}, 'datetime': '2022-08-27 11:30:00'}]
If I put them into a dataframe and then into CSV, I obtain this:
id isResult ... xG datetime
0 18209 True ... {'h': '1.42103', 'a': '1.7289'} 2022-08-07 13:00:00
1 18218 True ... {'h': '1.38785', 'a': '0.896038'} 2022-08-13 16:30:00
2 18231 True ... {'h': '2.01764', 'a': '1.52301'} 2022-08-22 19:00:00
3 18232 True ... {'h': '1.35887', 'a': '1.34359'} 2022-08-27 11:30:00
The part in braces is not split. Is there a way to get also this part split into pandas dataframe columns?
This is the code:
import pandas as pd
ta = [{'id': '18209', 'isResult': True, 'side': 'h', 'h': {'id': '89', 'title': 'Manchester United', 'short_title': 'MUN'}, 'a': {'id': '220', 'title': 'Brighton', 'short_title': 'BRI'}, 'goals': {'h': '1', 'a': '2'}, 'xG': {'h': '1.42103', 'a': '1.7289'}, 'datetime': '2022-08-07 13:00:00'}, {'id': '18218', 'isResult': True, 'side': 'a', 'h': {'id': '244', 'title': 'Brentford', 'short_title': 'BRE'}, 'a': {'id': '89', 'title': 'Manchester United', 'short_title': 'MUN'}, 'goals': {'h': '4', 'a': '0'}, 'xG': {'h': '1.38785', 'a': '0.896038'}, 'datetime': '2022-08-13 16:30:00'}, {'id': '18231', 'isResult': True,'side': 'h', 'h': {'id': '89', 'title': 'Manchester United', 'short_title': 'MUN'}, 'a': {'id': '87', 'title': 'Liverpool', 'short_title': 'LIV'}, 'goals': {'h': '2', 'a': '1'}, 'xG': {'h': '2.01764', 'a': '1.52301'}, 'datetime': '2022-08-22 19:00:00'}, {'id': '18232', 'isResult': True, 'side': 'a', 'h': {'id': '74', 'title': 'Southampton', 'short_title': 'SOU'}, 'a': {'id': '89', 'title': 'Manchester United', 'short_title': 'MUN'}, 'goals': {'h': '0', 'a': '1'}, 'xG': {'h': '1.35887', 'a': '1.34359'}, 'datetime': '2022-08-27 11:30:00'}]
df = pd.DataFrame(ta)
df.to_csv("G:\\stat.csv", header=True)
print(df)