[英]Creating a pandas.DataFrame wrapper that has a method to return a dataframe
[英]Return pandas.DataFrame when slice has one row result
考慮以下:
>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame(np.random.randn(5, 2), index=[100, 101, 101, 102, 103])
>>> idx = set(df.index)
>>> for id_ in idx:
... slice = df.loc[id_]
... # stuff with slice
>>>
我需要在for
循環中使用slice
做一些事情,但這些東西是基於slice
作為DataFrame
。 當有多個匹配記錄時, slice
是一個DataFrame
,否則是一個Series
。 我知道pandas.Series
有Series.to_frame
方法,但pandas.DataFrame
沒有(所以我不能只調用df.loc[id_].to_frame()
)。
測試和強制slice
到DataFrame
的最佳方法是DataFrame
?
(它是否真的像測試isinstance(df.loc[id_], pd.Series)
一樣簡單isinstance(df.loc[id_], pd.Series)
?)
您可以通過groupby
對象循環( level=0
):
for i, df1 in df.groupby(level=0):
print (df1)
0 1
100 -0.812375 -0.450793
0 1
101 1.070801 0.217421
101 -1.175859 -0.926117
0 1
102 -0.993948 0.586806
0 1
103 1.063813 0.237741
您應該通過為返回DataFrame
選擇double []
來更改您的解決方案:
idx = set(df.index)
for id_ in idx:
df1 = df.loc[[id_]]
print (df1)
0 1
100 -0.775057 -0.979104
0 1
101 -1.549363 -1.206828
101 0.445008 -0.173086
0 1
102 1.488947 -0.79252
0 1
103 1.838997 -0.439362
或者使用df[...]
條件df.index
:
...
for id_ in idx:
slice = df[df.index==id_]
print(slice)
輸出:
0 1
100 2.751189 1.978744
0 1
101 0.154483 1.646657
101 1.381725 0.982819
0 1
102 0.26669 0.032702
0 1
103 0.186235 -0.481184
您可以使用pd.Dataframe init方法強制變量slice成為pandas數據幀,如下所示:
for id_ in idx:
slice = pd.DataFrame(df.loc[id_])
print(type(slice))
輸出:
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
然后,您可以將變量視為循環內的Dataframes。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.