简体   繁体   中英

How to get common variables from dataframe through series in python

Example

Name  |    Numbers  |  Mean
Parth | 123         | 1
Nayan | 234         | 1.2
Paul  | 456         |2.3
Rashmi| 567         | 4.3
Anushka|678         | 2.3
Vignesh|789         | 5.6
Mark   | 345        | 9.4
Alavandar|123       | 1.2

I have a series of (3,) in part = ('Mark','Alavandar','Parth')

I need the data of just that part.

You can get a DataFrame with only the rows you're interested in like this:

>>> df = pd.DataFrame([['Parth', 123, 1],['foo',123,2]], columns=['Name','Numbers','Mean'])
>>> df
    Name  Numbers  Mean
0  Parth      123     1
1    foo      123     2
>>> df[df.Name == 'foo']
  Name  Numbers  Mean
1  foo      123     2

You can obviously expand the equality check to iterate over a list (or Series) of values. Remember to assign that operation to a new DataFrame if you will need it again since it returns a new one, like so:

names = ['foo', 'bar']
for name in names:
    df2 = df[df.Name == name]
>>>df2
  Name  Numbers  Mean
1  foo      123     2

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