How to filter some elements of a column in DataFrame based on the other columns value?

Viewed 40

Hi I have a DataFrame (see the screenshot) which want to filter some elemnts based on their values.

Announcement_data_file = "C:/Data/mini.csv"
Announcement_df = CSV.read(joinpath(Announcement_data_file), DataFrames.DataFrame)

f_ID = Announcement_df.Announcement[Announcement_df.Announcement.<100000]
s_ID = Announcement_df.Announcement[Announcement_df.Announcement.>100000]

The above codes work perfectly. However, the below ones give me Error. Why?

O_f = Announcement_df.Origin[f_ID]   

Or, also these two

Announcement_df.Origin['112264']
Announcement_df.Origin[112264] 

How can I filter Origins based on the valus of the Announcements??

enter image description here

1 Answers

Here are the reasons why your commands fail:

O_f = Announcement_df.Origin[f_ID]

fails, because f_ID is a vector [4199, 6698] and both these values are not in range of allowed indices of Announcement_df.Origin which is from 1 to 3.

Announcement_df.Origin['112264']

fails, because '112264' is an invalid expression in Julia. If you wanted to define a string then it should be "112264", but it would be also invalid as you cannot index vectors with strings.

Announcement_df.Origin[112264] 

fails, because you are trying to use 112264 index while allowed indices are from 1 to 3 as commented above.

Now, what I assume you want to do is:

Announcement_df.Origin[Announcement_df.Announcement .== 112264]
Related