简体   繁体   中英

How do I add sequential values to a pandas data frame?

I have a data frame like the following:

AF Important Info
Trial One
Trial Two

There is an entirely blank column, and it needs to be populated with data. I will have a near final data frame like the following:

AF Important Info
Trial One '70'
Trial Two '88'

How would I go about adding additional values to the "Important Info" column, such that the result is like this:

AF Important Info
Trial One '70', '99'
Trial Two '88', '71'

Thank you!

I would consider storing your data in long format like:

          AF Info
0  Trial One   70
1  Trial One   99
2  Trial Two   88
3  Trial Two   71

Then if you really want to see that info on one line, you can do:

>>> df.groupby('AF').agg(list)
               Info
AF
Trial One  [70, 99]
Trial Two  [88, 71]

# OR

>>> df.astype(str).groupby('AF').agg(', '.join)
             Info
AF
Trial One  70, 99
Trial Two  88, 71

And, this still allows you to perform other calculations per group...

>>> df.groupby('AF').mean()
           Info
AF
Trial One  84.5
Trial Two  79.5

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