Creating a subdata frame based on a value in a column

advertisements

I have following data frame in pandas. Now I want to generate sub data frame if I see a value in Activity column. So for example, I want to have data frame with all the data with Name A IF Activity column as value 3 or 5.

Name    Date           Activity
A       01-02-2015       1
A       01-03-2015       2
A       01-04-2015       3
A       01-04-2015       1
B       01-02-2015       1
B       01-02-2015       2
B       01-03-2015       1
B       01-04-2015       5
C       01-31-2015       1
C       01-31-2015       2
C       01-31-2015       2

So for the above data, I want to get df_A as

Name    Date           Activity
A       01-02-2015       1
A       01-03-2015       2
A       01-04-2015       3
A       01-04-2015       1

df_B as

B       01-02-2015       1
B       01-02-2015       2
B       01-03-2015       1
B       01-04-2015       5

Since Name C does not have 3 or 5 in the column Activity, I do not want to get this data frame.

Also, the names in the data frame can vary with each input file.

Once I have these data frame separated, I want to plot a time series.


You can use a dictionary comprehension to create a sub dataframe for each Name with an Activity value of 3 or 5.

active_names = df[df.Activity.isin([3, 5])].Name.unique().tolist()
dfs = {name: df.loc[df.Name == name, :] for name in active_names}

>>> dfs['A']
  Name        Date  Activity
0    A  01-02-2015         1
1    A  01-03-2015         2
2    A  01-04-2015         3
3    A  01-04-2015         1

>>> dfs['B']
  Name        Date  Activity
4    B  01-02-2015         1
5    B  01-02-2015         2
6    B  01-03-2015         1
7    B  01-04-2015         5