简体   繁体   中英

stack/unstack/pivot dataframe on python/pandas

I have a dataframe which looks like this:

<class 'pandas.core.frame.DataFrame'>  
Int64Index: 198300 entries, 0 to 198299  
Data columns (total 3 columns):  
var       198300  non-null values  
period    198300  non-null values  
value     141492  non-null values  
dtypes: float64(1), object(2)  

I'd like to change i from having three collumns (var, period, value) to having all values of the period variable as columns, the values in var as rows. i try using:

X.pivot(index='var', columns='period', values='value')    

But I get this error:

    raise ReshapeError('Index contains duplicate entries, '
pandas.core.reshape.ReshapeError: Index contains duplicate entries, cannot reshape  

But I've checked in excel, there are no duplicate entries... Any help out there? Thanks

To give this question an answer: usually when pandas objects that there are duplicate entries, it's right. To check this I often use

someseries.value_counts().head()

to see if one found its way in there.

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