使用Python将系列转换为数据帧
我正在使用Pandas,在Jupyter中从系列到数据帧的格式设置方面面临一些问题。我有一个这样结构的系列使用Python将系列转换为数据帧,python,pandas,jupyter,Python,Pandas,Jupyter,我正在使用Pandas,在Jupyter中从系列到数据帧的格式设置方面面临一些问题。我有一个这样结构的系列 0 {"province": "Paris", "city": "Paris", "countryCode": "FR", "floor": null, "country": "France", "
0 {"province": "Paris",
"city": "Paris", "countryCode": "FR", "floor": null, "country":
"France", "route": "RUE MONGE", "extra": null, "coordinates":
[2.35242, 48.84477], "streetNumber": "55", "locationType": null,
"postalCode": "75005"}
1 {"province": null, "city": "Paris",
"countryCode": "FR", "floor": "CPO_BELI_floor_1482430978123",
"country": "France", "route": "PLACE DU PANTHEON", "extra": null,
"coordinates": [2.345032, 48.845715], "streetNumber": "17",
"locationType": "OUTDOOR", "postalCode": "75005"}
2 {"province": null, "city": "Paris", "countryCode": "FR", "floor":
"CPO_BELI_floor_1482430978123", "country": "France", "route": "RUE DU
BAC", "extra": null, "coordinates": [2.327753, 48.857124],
"streetNumber": "35", "locationType": "OUTDOOR", "postalCode":
"75007"}
我运行此代码是为了将其转换为dataframe,但它没有将该系列拆分为正确的列:
pd.DataFrame(data['fields.geolocation'], index=data.index)
您已关闭,需要将每一行转换为
list
s:
df = pd.DataFrame(data['fields.geolocation'].values.tolist(), index=data.index)
样本:
a = [{"province": "Paris", "city": "Paris", "countryCode": "FR", "floor": 'null', "country": "France", "route": "RUE MONGE", "extra": 'null', "coordinates": [2.35242, 48.84477], "streetNumber": "55", "locationType": 'null', "postalCode": "75005"} ,
{"province": 'null', "city": "Paris", "countryCode": "FR", "floor": "CPO_BELI_floor_1482430978123", "country": "France", "route": "PLACE DU PANTHEON", "extra": 'null', "coordinates": [2.345032, 48.845715], "streetNumber": "17", "locationType": "OUTDOOR", "postalCode": "75005"} ,
{"province": 'null', "city": "Paris", "countryCode": "FR", "floor": "CPO_BELI_floor_1482430978123", "country": "France", "route": "RUE DU BAC", "extra": 'null', "coordinates": [2.327753, 48.857124], "streetNumber": "35", "locationType": "OUTDOOR", "postalCode": "75007"}]
s = pd.Series(a, index=[2,3,5])
print (s)
2 {'province': 'Paris', 'city': 'Paris', 'countr...
3 {'province': 'null', 'city': 'Paris', 'country...
5 {'province': 'null', 'city': 'Paris', 'country...
dtype: object
尝试将
pd.concat
与axis=1
()一起使用:
这是您的系列:
A = {"province": "Paris", "city": "Paris", "countryCode": "FR", "floor": None, "country": "France", "route": "RUE MONGE", "extra": None, "coordinates": [2.35242, 48.84477], "streetNumber": "55", "locationType": None, "postalCode": "75005"}
B = {"province": None, "city": "Paris", "countryCode": "FR", "floor": "CPO_BELI_floor_1482430978123", "country": "France", "route": "PLACE DU PANTHEON", "extra": None, "coordinates": [2.345032, 48.845715], "streetNumber": "17", "locationType": "OUTDOOR", "postalCode": "75005"}
C = {"province": None, "city": "Paris", "countryCode": "FR", "floor": "CPO_BELI_floor_1482430978123", "country": "France", "route": "RUE DU BAC", "extra": None, "coordinates": [2.327753, 48.857124], "streetNumber": "35", "locationType": "OUTDOOR", "postalCode": "75007"}
A_series = pd.Series(A)
B_series = pd.Series(B)
C_series = pd.Series(C)
通过这种方式,您可以创建理想的数据帧
df = pd.concat([A_series, B_series, C_series], axis=1)
type(df)
pandas.core.frame.DataFrame
希望这能有所帮助。它不会分割序列,因为序列是一个只有一列的数据帧。因此,该系列的列将是数据帧的列。如果要将序列索引作为列添加到数据帧中,则需要转置序列。
df = pd.concat([A_series, B_series, C_series], axis=1)
type(df)
pandas.core.frame.DataFrame