Dataframe Comprehension in Pandas Python to create new Dataframe -


i pretty new pandas create 1 dataframe based on condition name mel. looks new dataframe pointer old 1 (based on index number printed out).

i'm looking equivalent of this:

babydataset = [['bob', 968], ['jessica', 155], ['mary', 77], ['john', 578], ['mel', 973]] filtered_list = [x x in babydataset if x[0] == 'mel'] print filtered_list df = pd.dataframe(data=filtered_list, columns=['names', 'births']) print df 

mycode:

import pandas pd  babydataset = [['bob', 968], ['jessica', 155], ['mary', 77], ['john', 578], ['mel', 973]] #create dataframe df = pd.dataframe(data=babydataset, columns=['names', 'births'])  #create new dataframe bob new_df = df.ix[['mel' in x x in df['names']]] print new_df 

no need walk df, pass boolean condition filter df:

in [216]: new_df = df[df['names']=='mel'] new_df  out[216]:   names  births 4   mel     973 

edit

to reset index call reset_index(), whether new_df reference orig df or not, it's not:

in [224]: new_df = df[df['names']=='mel'] new_df = new_df.reset_index() new_df  out[224]:    index names  births 0      4   mel     973  in [225]:     new_df['names'] = 'asdas' df  out[225]:      names  births 0      bob     968 1  jessica     155 2     mary      77 3     john     578 4      mel     973 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -