Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

python 3.x - Pandas select unique values from column

I was able to ingest a csv in jupyter notes by doing this :

csvData= pd.read_csv("logfile.csv")

My data looks like this:

event_timestamp ip  url 
2018-01-10 00:00 111.111.111.111 http://webpage1.com
2018-01-10 00:00 222.222.222.222 http://webpage2.com
...
..
.

I got a list of unique ips:

list_ips = csvData("[ip]")

What I'm trying to do is get a unique. Normally I would do:

list_ips.unique()

But in this case I get this error:

AttributeError: 'DataFrame' object has no attribute 'unique'

(I can use list_ips.head() and it will list a few IPs but it's not a unique list)

Thanks

EDIT My problem is I actually had:

list_ips = csvData([["ip"]]) 

So I removed 1 set of brackets so it became:

list_ips = csvData(["ip"]) 

Then I was able to follow Wen's example and do:

list_ips.unique().tolist()

Output:

['111.111.111.111','222.222.222.222'...]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to select the column correctly then apply unique

csvData['ip'].unique().tolist()
Out[677]: ['111.111.111.111', '222.222.222.222']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...