![](/img/trans.png)
[英]convert pandas series AND dataframe objects to a numpy array
[英]How to convert pandas series with numpy array values to dataframe
series = pd.Series([np.array([1,2,3,4]), np.array([5,6,7,8]), np.array([9,10,11,12])], index=['file1', 'file2', 'file3'])
file1 [1, 2, 3, 4]
file2 [5, 6, 7, 8]
file3 [9, 10, 11, 12]
如何將其擴展為df_concatenated
形式的數據df_concatenated
:
0 1 2 3
file1 1 2 3 4
file2 5 6 7 8
file3 9 10 11 12
series
是從以下形式的其他數據框中獲得的: 數據幀:
0 1
file slide
file1 1 1 2
2 3 4
file2 1 5 6
2 7 8
file3 1 9 10
2 11 12
通過將“文件”索引與列的連接分組。
def concat_sublevel(data):
return np.concatenate(data.values)
series = data.groupby(level=[0]).apply(concat_sublevel)
可能有人看到將數據幀data
為df_concatenated
的更好方法。
警告。 對於不同的file
值, slide
子索引可以具有不同數量的值。 在這種情況下,我需要重復一行,以在所有結果行中獲得相同的尺寸
您可以嘗試使用記錄中的pandas Dataframe
pd.DataFrame.from_records(series.values,index=series.index)
日期:
0 1 2 3
file1 1 2 3 4
file2 5 6 7 8
file3 9 10 11 12
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.