简体   繁体   中英

Create a new data frame of counts from a list corresponding to column index values from a different data frame

I have two unique lists like:

a = [12, 12, 12, 3, 4, 5]
b = [1, 2, 4, 5, 6, 12, 4, 7, 9, 2, 3, 5, 6]

df.columns
Index(['lep', 'eta', 'phi', 'missing energy magn', 'missing energy phi', 'jet]) etc

(columns is longer than I wrote here, obviously).

Lists a and b correspond to column index values.

I want to create a new dataframe with the following columns:

Index Value, Column Name, Count of a, Count of b

So if '12' appears three times in list a,, twice in list b and corresponds to 'foo' in df, the corresponding row in the new dataframe would return: 12, foo, 3, 2

I know how to get counts of list values by four loops but I'm not sure how to target the index value

Desired output would be something like (based on the data above):

new_df.head()

-index, name, count_a, count_b

  • 0 lep, 0, 0
  • 1, eta, 0, 1
  • 2, phi, 0, 2
  • 3, missing energy mag, 1, 1
  • 4 massing energy phi, 1, 1

You can use count() to count number of occurences of element in list and enumerate() to iterate over list and keep a count of iterations.

So your code becomes:

import pandas as pd

a = [12, 12, 12, 3, 4, 5]
b = [1, 2, 4, 5, 6, 12, 4, 7, 9, 2, 3, 5, 6]


elements = ['lep', 'eta', 'phi', 'missing energy magn', 'missing energy phi', 'jet']  # it is not necessary to declare it as an index, it is a simple list

df_data = []
for i, el in enumerate(elements):

    tmp_dict = {
        'name': el,
        'count_a': a.count(i),
        'count_b': b.count(i)
    }

    df_data.append(tmp_dict)

df = pd.DataFrame(df_data)
print(df)

Output will be:

                  name  count_a  count_b
0                  lep        0        0
1                  eta        0        1
2                  phi        0        2
3  missing energy magn        1        1
4   missing energy phi        1        2
5                  jet        1        2

This approach works regardless of the number of elements in the list obviously.

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