简体   繁体   中英

Return pandas.DataFrame when slice has one row result

Consider the following:

>>> 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
>>>

I need to do stuff with slice within the for loop but that stuff is predicated on slice being a DataFrame . slice is a DataFrame when there are more than one matching records, but a Series otherwise. I know pandas.Series has the Series.to_frame method but pandas.DataFrame does not (so I cannot just call df.loc[id_].to_frame() ).

What is the best way to test and coerce slice into a DataFrame ?

(Is it really as simple as testing if isinstance(df.loc[id_], pd.Series) ?)

You can loop by groupby object by index ( 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

Your solution should be changed by selecting double [] for return DataFrame :

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

Or use df[...] conditioning df.index :

...
for id_ in idx:
     slice = df[df.index==id_]
     print(slice)

Output:

            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

You can force the variable slice to be a pandas dataframe by using the pd.Dataframe init method as follows:

for id_ in idx:
    slice = pd.DataFrame(df.loc[id_])
    print(type(slice))

output:

<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>

Then you can treat the variables as Dataframes inside the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM