python - Extracting values from pandas DataFrame using a pandas Series -


i have pandas series contains key-value pairs, key name of column in pandas dataframe , value index in column of dataframe.

for example:

series: series

then in dataframe: dataframe

therefore, dataframe want extract value @ index 12 dataframe 'a', 435.81 . want put these values series, { 'a': 435.81 , 'aap': 468.97,...}

my reputation low can't post images images instead of links (can fix this? thanks!)

i think indexing you're looking for.

pd.series(np.diag(df.loc[ser,ser.axes[0]]), index=df.columns) 

df.loc allows index based on string indices. rows given values in ser (first positional argument in df.loc) , column location labels of ser (i don't know if there better way labels series ser.axes[0]). values want along main diagonal of result, take diagonal , associate them column labels.

the indexing gave before works if dataframe uses integer row indices, or if data type of series values matches dataframe row indices. if have dataframe non-integer row indices, still want values based on integer rows, use following (however, indices series must within range of dataframe, not case 'aal' being 1758 , 12 rows, example):

pd.series(np.diag(df.iloc[ser,:].loc[:,ser.axes[0]]), index=df.columns) 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -