将 Pandas 系列列表转换为单个 Pandas DataFrame
Converting a List of Pandas Series to a single Pandas DataFrame
我在我的数据集上使用 statsmodels.api
。我有熊猫系列的清单。熊猫系列有键值对。键是列的名称,值包含数据。但是,我有一个系列列表,其中重复了键(列名)。我想将 pandas 系列列表中的所有值保存到单个数据框中,其中列名是熊猫系列的键。列表中的所有系列都具有相同的键。我想将它们保存为单个数据框,以便我可以将数据框导出为 CSV。知道如何将键保存为 df 的列名,然后让值填充其余信息。
列表中的每个系列 returns 如下所示:
index 0 of the list: <class 'pandas.core.series.Series'>
height 23
weight 10
size 45
amount 9
index 1 of the list: <class 'pandas.core.series.Series'>
height 11
weight 99
size 25
amount 410
index 2 of the list: <class 'pandas.core.series.Series'>
height 3
weight 0
size 115
amount 92
我希望能够读取数据帧,以便将这些值保存为以下内容:
DataFrame:
height weight size amount
23 10 45 9
11 11 25 410
3 3 115 92
这不是最有效的方法,但可以解决问题:
import pandas as pd
series_list =[ pd.Series({ 'height': 23,
'weight': 10,
'size': 45,
'amount': 9
}),
pd.Series({ 'height': 11,
'weight': 99,
'size': 25,
'amount': 410
}),
pd.Series({ 'height': 3,
'weight': 0,
'size': 115,
'amount': 92
})
]
pd.DataFrame( [series.to_dict() for series in series_list] )
pd.DataFrame(data=your_list_of_series)
创建新的 DataFrame 时,pandas 将接受数据参数的系列列表。您系列的索引将成为 DataFrame 的列名。
您是否尝试过在系列列表中调用 pd.DataFrame()
?那应该行得通。
import pandas as pd
series_list = [
pd.Series({
'height': 23,
'weight': 10,
'size': 45,
'amount': 9
}),
pd.Series({
'height': 11,
'weight': 99,
'size': 25,
'amount': 410
}),
pd.Series({
'height': 3,
'weight': 0,
'size': 115,
'amount': 92
})
]
df = pd.DataFrame(series_list)
print(df)
df.to_csv('path/to/save/foo.csv')
输出:
height weight size amount
0 23 10 45 9
1 11 99 25 410
2 3 0 115 92
我在我的数据集上使用 statsmodels.api
。我有熊猫系列的清单。熊猫系列有键值对。键是列的名称,值包含数据。但是,我有一个系列列表,其中重复了键(列名)。我想将 pandas 系列列表中的所有值保存到单个数据框中,其中列名是熊猫系列的键。列表中的所有系列都具有相同的键。我想将它们保存为单个数据框,以便我可以将数据框导出为 CSV。知道如何将键保存为 df 的列名,然后让值填充其余信息。
列表中的每个系列 returns 如下所示:
index 0 of the list: <class 'pandas.core.series.Series'>
height 23
weight 10
size 45
amount 9
index 1 of the list: <class 'pandas.core.series.Series'>
height 11
weight 99
size 25
amount 410
index 2 of the list: <class 'pandas.core.series.Series'>
height 3
weight 0
size 115
amount 92
我希望能够读取数据帧,以便将这些值保存为以下内容:
DataFrame:
height weight size amount
23 10 45 9
11 11 25 410
3 3 115 92
这不是最有效的方法,但可以解决问题:
import pandas as pd
series_list =[ pd.Series({ 'height': 23,
'weight': 10,
'size': 45,
'amount': 9
}),
pd.Series({ 'height': 11,
'weight': 99,
'size': 25,
'amount': 410
}),
pd.Series({ 'height': 3,
'weight': 0,
'size': 115,
'amount': 92
})
]
pd.DataFrame( [series.to_dict() for series in series_list] )
pd.DataFrame(data=your_list_of_series)
创建新的 DataFrame 时,pandas 将接受数据参数的系列列表。您系列的索引将成为 DataFrame 的列名。
您是否尝试过在系列列表中调用 pd.DataFrame()
?那应该行得通。
import pandas as pd
series_list = [
pd.Series({
'height': 23,
'weight': 10,
'size': 45,
'amount': 9
}),
pd.Series({
'height': 11,
'weight': 99,
'size': 25,
'amount': 410
}),
pd.Series({
'height': 3,
'weight': 0,
'size': 115,
'amount': 92
})
]
df = pd.DataFrame(series_list)
print(df)
df.to_csv('path/to/save/foo.csv')
输出:
height weight size amount
0 23 10 45 9
1 11 99 25 410
2 3 0 115 92