简体   繁体   中英

Concatenate pandas objects where one is a one-dimensional pd ndarray

I have a long pd dataframe mydf and a one-dimensional ndarray, with the same columns names and type, created like this:

Row = pd.Series(0, mydf.columns)
Row = mydf.iloc[index]

I want to add the Row in a defined position i in the middle of the dataframe mydf . Thereofre I use following:

mydf = pd.concat([mydf.head(idx), Row , mydf.tail(len(mydf) - idx)])

I always get the following warning and then the codes doesn't run:

 '>' not supported between instances of 'int' and 'str', sort order is undefined for incomparable objects 
result = result.union(other)

How can the error come if my Row has exactly the same format like the dataframe? How to fix this ?

Thx.

What you are getting is just a warning the Row is begining concatenated to the dataframe not in a proper way.

Row is a series and you are trying to concat series between two dataframes. Series has index dtype object (string) and df has index dtype int. Index will be sorted usually when concatenated. Now the function doesn't know how to sort the mixed dtype index. So you get that warning.

So better convert Row to dataframe to avoid this error ie

ndf = pd.concat([mydf.head(index), pd.DataFrame(Row).T, mydf.tail(len(mydf) - index)])

For Example consider the dataframe mydf

id  offset  code
0   1       3    21
1   1       3    24
2   1       5    21
index= 2 
Row = mydf.iloc[index]
df = pd.concat([mydf.head(index), Row , mydf.tail(len(mydf) - index)])

/python3.5/site-packages/pandas/core/indexes/api.py:77: RuntimeWarning: unorderable types: str() < int(), sort order is undefined for incomparable objects
result = result.union(other)
id  offset  code     0
0       1.0     3.0  21.0   NaN
1       1.0     3.0  24.0   NaN
id      NaN     NaN   NaN   1.0
offset  NaN     NaN   NaN   5.0
code    NaN     NaN   NaN  21.0
2       1.0     5.0  21.0   NaN
df = pd.concat([mydf.head(index), pd.DataFrame(Row).T , mydf.tail(len(mydf) - index)])
id  offset  code
0   1       3    21
1   1       3    24
2   1       5    21
2   1       5    21

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