pandas


Need to loop over pandas series to find indices of variable


I have a dataframe and a list. I would like to iterate over elements in the list and find their location in dataframe then store this to a new dataframe
my_list = ['1','2','3','4','5']
df1 = pd.DataFrame(my_list, columns=['Num'])
dataframe : df1
Num
0 1
1 2
2 3
3 4
4 5
dataframe : df2
0 1 2 3 4
0 9 12 8 6 7
1 11 1 4 10 13
2 5 14 2 0 3
I've tried something similar to this but doesn't work
for x in my_list:
i,j= np.array(np.where(df==x)).tolist()
df2['X'] = df.append(i)
df2['Y'] = df.append(j)
so looking for a result like this
dataframe : df1 updated
Num X Y
0 1 1 1
1 2 2 2
2 3 2 4
3 4 1 2
4 5 2 0
any hints or ideas would be appreciated
Instead of trying to find the value in df2, why not just make df2 a flat dataframe.
df2 = pd.melt(df2)
df2.reset_index(inplace=True)
df2.columns = ['X', 'Y', 'Num']
so now your df2 just looks like this:
Index X Y Num
0 0 0 9
1 1 0 11
2 2 0 5
3 3 1 12
4 4 1 1
5 5 1 14
You can of course sort by Num and if you just want the values from your list you can further filter df2:
df2 = df2[df2.Num.isin(my_list)]

Related Links

Using value_counts in pandas with condtions
Filter multiples in a pandas dataframe
plotting histograms in pandas
How to map values in a list to a pandas dataframe with binary values
loosing rows in pandas dataframe after apply method that doesnt delete row
Memory is not released when taking a small slice of a DataFrame
How to pandas groupby and pivot_table to have pivto tables look like Excel's
Pandas: Series of quotients of adjacent elements
Zodb process Killed after reading multiple fs
pandas group By select columns
Pandas fill cells in a column with NaN values, derive the value from other cells in the row
Merge files based on a date range?
pandas pivot_table: values per column instead of columns per value
NaNs disappear after saving to excel file
Is there any way to convert columns's value from nonconsecutive numbers to 1-started consecutive numbers?
pandas astype categories not working

Categories

HOME
vbscript
keycloak
reflection
mfc
tinymce
cplex
infragistics
graphql
v8
c#-2.0
baqend
ravendb
uber-api
multiple-records
cross-validation
gnupg
row
n-gram
vaadin7
clojurescript
reactive-cocoa
evopdf
samoa
firefox-webextensions
dbext
physics-engine
jndi
emgucv
qhull
cas
tinymce-4
socialengine
bcd
xquery-3.0
semantic-versioning
typed.js
kendo-ui-grid
uiswipegesturerecognizer
language-concepts
xcode-extension
libraries
sharefile
unspecified
ensembles
mapzen
janrain
s
executenonquery
skeleton-css-boilerplate
user-accounts
termination
python-c-api
clean-architecture
sage-one
dropbox-php
nativeapplication
jspdf-autotable
dwscript
blacklist
eventkit
elements
etsy
player
qpid
disque
nsviewcontroller
pagedlist
qgraphicsview
ptrace
revolution-r
separator
thredds
ruby-2.2
qdialog
wireshark-dissector
method-parameters
p2
fpml
code-access-security
zend-route
eol
wp-query
poller
app42
viadeo
dexterity
tidy
cos
shellexecute
back-stack
html4
mqx
lcs
wsdl-2.0
free-variable
e4x
multipage
objective-c-2.0
getmessage
online-compilation
enter
nsdatecomponents
gcj
imac
rescale
anti-piracy
noscript
genealogy
w3wp
signal-handling
ntvdm.exe

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App