简体   繁体   中英

In-Place Update of Values in Pandas Dataframe

I'm new to pandas. I've built a dataframe where all the values are lists that look like [Year, Datapoint] (eg [2013, 37722.322] or [1998, 32323.232). How do I get rid of the year value and just replace the list in each cell in the dataframe with just the float datapoint?

Thanks so much.

You mean like this?

In [16]:

import operator
In [17]:

DF=pd.DataFrame({'Val':[[2013, 37722.322],[1998, 32323.232]]})
In [18]:

print DF
                 Val
0  [2013, 37722.322]
1  [1998, 32323.232]

[2 rows x 1 columns]
In [19]:

DF['Val2']=DF.Val.apply(operator.itemgetter(-1), axis=1)
In [20]:

print DF
                 Val       Val2
0  [2013, 37722.322]  37722.322
1  [1998, 32323.232]  32323.232

[2 rows x 2 columns]

To do this to all your columns: DF.applymap(operator.itemgetter(-1)) per @DSM's suggestion.

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