[AstroPy] anyone using Pandas?

Thøger Rivera-Thorsen trive@astro.su...
Sat Dec 15 05:44:18 CST 2012


Hi;

It still depends. Your MultiIndex can be made from lists, tuples or 
arrays; I usually use arrays since I'm familiar with them from using 
NumPy.  For a 3-level index, you need to have 3 1D-arrays of equal 
length, that civer alle the atomic nr. - Ion - Level combinations you 
want covered. Toy example; 3 atoms with three levels each, atoms denoted 
by number and levels by letters. Let's say the two first atoms have 3, 
and your third atom five levels. Then your arrays should be:
atoms = sp.array(['1', '1', '1', '2', '2', '2', '3', '3', '3', '3', '3'])
levels  = sp.array(['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'd', 'e'])

idx = pd.MultiIndex.from_arrays([atoms, levels], names=['atoms', 'levels'])

Your data should then be a 11x2 array with energy and g as the columns. 
You then build the array as:

MyDF = pd.DataFrame(dataarray, index=idx, columns=['Nrg_lvl', 'g.'])


That gives:

In [22]: print MyDF

                Nrg_lvl        g.

atoms levels

1     a       0.052846  0.533835

       b       0.185949  0.064069

       c       0.384630  0.646803

2     a       0.835958  0.392594

       b       0.016399  0.165862

       c       0.300874  0.975590

3     a       0.124640  0.815488

       b       0.590613  0.749555

       c       0.284481  0.299149

       d       0.104408  0.723406

       e       0.733087  0.730055


(Here I have just used random numbers for the data).

 From here, you can do all the Pandas goodness you want:

In [23]: print MyDF.xs('b', level=1)

         Nrg_lvl        g.

atoms

1      0.185949  0.064069

2      0.016399  0.165862

3      0.590613  0.749555


Once you have the basic structure built, it is easy to extend either by 
concatenating or by using the set_value function. However, most of these 
operations are returning a modified copy rather than in-place 
operations, so better build as much of the DataFrame as possible in one 
go, if you're concerned about memory overhead. Åandas doesn't seem to be 
designed to gradually build its data structures.

Cheers;

Thøger


On 12/15/2012 05:52 AM, Wolfgang Kerzendorf wrote:
> Sorry before this didn't go to the list:
>
> Hey guys,
>
> I'm slowly understanding better what pandas is about. The object I'm representing in a pandas data frame is an atomic database.
> Each line in there has atomic_number, ion_number, level_number, energy, g. So I have created a Pandas dataFrame and then set the index to atomic_number, ion_number, level_number. Now I want to make a new DataFrame where atomic_number in (6, 7, 8, 9) - but it is an index. how do I do that?
>
> Cheers
>     W
> On 2012-12-14, at 8:15 PM, Thøger Rivera-Thorsen <trive@astro.su.se> wrote:
>
>> Like Tyler said, can you be a bit more specific about what you want to
>> obtain?
>> What is your starting point, and where do you want to go from there?
>>
>> I've been looking quite a bit into multiindexing lately, and it is very
>> handy but it does have some caveats.
>>
>>
>>
>>
>>
>>
>> On 12/14/2012 09:04 PM, Wolfgang Kerzendorf wrote:
>>> Hey guys,
>>>
>>> I'm trying to play around with pandas. Currently I have a look at join and it always seems to copy the data. I believe I want to use advanced indexing, but am not quite sure how to do that: any Pandas experts here?
>>>
>>> Cheers
>>>     Wolfgang
>>> _______________________________________________
>>> AstroPy mailing list
>>> AstroPy@scipy.org
>>> http://mail.scipy.org/mailman/listinfo/astropy
>> _______________________________________________
>> AstroPy mailing list
>> AstroPy@scipy.org
>> http://mail.scipy.org/mailman/listinfo/astropy
> _______________________________________________
> AstroPy mailing list
> AstroPy@scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy



More information about the AstroPy mailing list