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

Pandas.to_csv thousand separator
Annotating a graph with certain values of another series (Index is datetime)
Pandas rolling sum on string column
pandas apply() with and without lambda
Pandas read_html to retrieve Table
pandas: reshape dataframe for stacked bar plot
Change values in a column from a list
Pandas: How to Return Max Value in Multiindex
cx freeze module not correctly installed
Counting occurences from a dict and pandas
Calculating mean of a specific column by specific rows
Value counts per period, taking prior values into account
mapping values from another pandas df
Tensorflow: Cannot allocate buffer larger than kint32max for StringOutputStream
using assign and lambda to combine year and month columns into 1 date column
Apply an element-wise function on a pandas dataframe with index and column values as inputs

Categories

HOME
pypi
mediawiki
relative-path
onedrive
packages
dxl
azure-media-services
mouse
paradox
multiple-records
autotools
ssl-client-authentication
here-api
google-cloud-spanner
remote-access
circuit
saxon
fatal-error
evopdf
oracle-coherence
oxyplot
physics-engine
tokenize
excel-2007
cas
intel-pin
applozic
minitab
log4js-node
dbclient
kendo-datasource
fabric8
yadcf
lxd
avcapturesession
force-layout
environment-modules
catalog
dosbox
x11-forwarding
swift3.0.2
glew
acoustics
cubic-spline
xml-documentation
azure-application-gateway
youcompleteme
revapi
pango
python-c-api
yii2-extension
aurelia-validation
recursive-datastructures
carrot
lift-json
mcafee
migradoc
rotativa
radtreelist
multi-level
transmitfile
hill-climbing
superstack
python-stackless
dlna
dataview
ipconfig
energy
vhd
msys
sdhc
mutation-observers
fouc
pundit
angular-leaflet-directive
php-ci
system32
pretty-print
e10s
wordpress-theme-customize
flask-cors
terminfo
eol
sgen
contenttype
elliptic-curve
p4java
marmalade
poller
bundles
mcts
sitemesh
runtime.exec
apc
cos
batterylevel
padarn
comaddin
javaspaces
dbproviderfactories
hgsubversion
doh
hirefire
self-extracting
propagation
remember-me
perfect-hash
phonon
premature-optimization
scripting-languages
dbisam

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