Skip to content Skip to sidebar Skip to footer

Filter Item Iteration In Dataframe (with For Or Any Other)?

I have the following dataframe: d = pd.DataFrame([['A', 1989, 100], ['A', 1990, 200], ['A', 2017, 100], ['B', 1989, 500],

Solution 1:

Option 1 Perform a groupby on Univers, since you need to save each group.

fori, gindf.groupby('Univers'):
    g.to_csv('{}.csv'.format(i))

This generates 3 files -

A.csv

  Univers  year  amount
0A19891001A19902002A2017100

B.csv

  Univers  year  amount
3B19895004B1990200

C.csv

  Univers   year  amount
5       C   19902006       C  19870400

Option 2 Another alternative would be to call pd.Series.unique and then filter on this condition -

for v in df.Univers.unique():
    df[df.Univers == v].to_csv('{}.csv'.format(i))

Which does the same thing. You can also use query/eval to perform filtering.

Solution 2:

This is a simple (and probably not optimized) way:

key_name = 'Univers'
univers = set(d[key_name])
for uni in univers:
    print d[d[key_name] == uni]

Output:

Universyearamount0A1989     1001A1990     2002A2017     100Universyearamount5C1990     2006C19870400Universyearamount3B1989     5004B1990     200

Solution 3:

Im assuming that you have a list of acceptable values for "Univer" in another dataframe lets say x...

x Univers Col2

A test1

B test2

C test3

You can join both the dataframes and filter out the rows which you need. Approx syntax result = pd.concat([d, x], on='Univers').. Is that what you wanted?

Post a Comment for "Filter Item Iteration In Dataframe (with For Or Any Other)?"