Stack Overflow Asked by user3579222 on January 5, 2021
I try to create a query such as
traffic.groupby([round(traffic.a),traffic.r])[["sn","sbr"]].nunique().loc[a==2,:]
The last loc statement does not work (a==2
): "name ‘a’ is not defined.
How can I filter for rows on such an anonymous dataframe?
** Update **
Here is an example
a=[1,2,2,3]
r=[2,2,3,5]
sn=[1,1,2,2]
sbr=[2,2,2,3]
traffic=pd.DataFrame([a,r,sn,sbr],columns=["a","r","sn","sbr"])
traffic.groupby([round(traffic.a),traffic.r])[["sn","sbr"]].nunique().loc[a==2,:]
traffic looks like the following:
a r sn sbr
0 1 2 2 3
1 2 2 3 5
2 1 1 2 2
3 2 2 2 3
This might help:
import pandas as pd
a = [1,2,2,3]
r = [2,2,3,5]
sn = [1,1,2,2]
sbr = [2,2,2,3]
traffic = pd.DataFrame([a,r,sn,sbr],columns=["a","r","sn","sbr"])
traffic.groupby([round(traffic.a),traffic.r])[["sn","sbr"]].nunique().T[2].T
The output is:
sn sbr
r
2 2 2
Correct answer by sotmot on January 5, 2021
I wish I could see your dataframe structure! But the way that I usually query a row without a name (I assume when you're saying "anonymous", your dataset doesn't have headers) is to use iloc
.
For example if I want to return the first two rows I would say:
dataframe.iloc[[0, 1]]
Or if I need to return the first two rows for a specific columns let's say column number 4
dataframe.iloc[[0, 1], 3]
I hope this would help!
Answered by Mokhless on January 5, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP