I am new to python and pandas dataframe, I have this list/dict output and I need to combine it in one dataframe. I'm having trouble combining it in one dataframe and saving it in one csv.
data = [{"TimePeriod_Start":"2022-10-01","TimePeriod_End":"2022-10-02","PLATFORM":"TEST1","A":"0.30001","B":"0.0002","C":"0.00003","D":"0.000047","D":"0.0003","E":"0.00006"}][{"TimePeriod_Start":"2022-10-01","TimePeriod_End":"2022-10-02","PLATFORM":"TEST2","C":"0.700003","D":"00001","D":"00005","E":"00005","F":"0.000008","G":"000.245","H":"1.111258","I":"0000.1236"}]
CodePudding user response:
a = [{"TimePeriod_Start":"2022-10-01","TimePeriod_End":"2022-10-02","PLATFORM":"TEST1","A":"0.30001","B":"0.0002","C":"0.00003","D":"0.000047","D":"0.0003","E":"0.00006"}]
b = [{"TimePeriod_Start":"2022-10-01","TimePeriod_End":"2022-10-02","PLATFORM":"TEST2","C":"0.700003","D":"00001","D":"00005","E":"00005","F":"0.000008","G":"000.245","H":"1.111258","I":"0000.1236"}]
I'm not exactly sure you want which dataframe to combine with. Is this what you want?
pd.DataFrame(a b)
TimePeriod_Start TimePeriod_End PLATFORM A B C D E F G H I
0 2022-10-01 2022-10-02 TEST1 0.30001 0.0002 0.00003 0.0003 0.00006 NaN NaN NaN NaN
1 2022-10-01 2022-10-02 TEST2 NaN NaN 0.700003 00005 00005 0.000008 000.245 1.111258 0000.1236
CodePudding user response:
This should work
import pandas as pd
data = [
{
"TimePeriod_Start": "2022-10-01",
"TimePeriod_End": "2022-10-02",
"PLATFORM": "TEST1",
"A": "0.30001",
"B": "0.0002",
"C": "0.00003",
"D": "0.0003",
"E": "0.00006",
}
]
data2 = [
{
"TimePeriod_Start": "2022-10-01",
"TimePeriod_End": "2022-10-02",
"PLATFORM": "TEST2",
"C": "0.700003",
"D": "00005",
"E": "00005",
"F": "0.000008",
"G": "000.245",
"H": "1.111258",
"I": "0000.1236",
}
]
d = data data2
df = pd.DataFrame.from_records(d).to_csv("filePath")
Or simply
pd.DataFrame(data data2)
CodePudding user response:
You can use:
pd.DataFrame.from_dict(data[0], orient='index').T
CodePudding user response:
There are missing square brackets and comma. True format must be like this:
data = [[{"TimePeriod_Start":"2022-10-01","TimePeriod_End":"2022-10-02","PLATFORM":"TEST1","A":"0.30001","B":"0.0002","C":"0.00003","D":"0.000047","D":"0.0003","E":"0.00006"}],[{"TimePeriod_Start":"2022-10-01","TimePeriod_End":"2022-10-02","PLATFORM":"TEST2","C":"0.700003","D":"00001","D":"00005","E":"00005","F":"0.000008","G":"000.245","H":"1.111258","I":"0000.1236"}]]
df = pd.json_normalize(data)[0].apply(pd.Series)
'''
| | TimePeriod_Start | TimePeriod_End | PLATFORM | A | B | C | D | E | F | G | H | I |
|---:|:-------------------|:-----------------|:-----------|----------:|---------:|---------:|-------:|------:|--------:|--------:|----------:|---------:|
| 0 | 2022-10-01 | 2022-10-02 | TEST1 | 0.30001 | 0.0002 | 3e-05 | 0.0003 | 6e-05 | nan | nan | nan | nan |
| 1 | 2022-10-01 | 2022-10-02 | TEST2 | nan | nan | 0.700003 | 5 | 5 | 8e-06 | 0.245 | 1.11126 | 0.1236 |
'''