pandas, how to filter dataframe by column value

GoingMyWay

I have a DataFrame like this

>>> df
    id    name    score    subject
    0001   'bob'    100    'math'
    0001   'bob'     67    'science'
    0001   'bob'     63    'bio'
    0002  'jack'     67    'math'
    0002  'jack'     98    'science' 
    0002  'jack'     90    'bio'
    0003  'jack'     60    'math'
    0003  'jack'     78    'science' 
    0003  'rose'     87    'bio'

I want to filter every id's data into a new DataFrame and write to an Excel file based on its id. So, the above df will be filtered into 3 DataFrames whose ids are 0001, 0002 and 0003, and all the DataFrames will be written to individual excel files.

Tasos

First, get a list of the unique ID values

uniquevalues = np.unique(df[['id']].values)

Then iterate on it and export each dataframe with those IDs in a CSV file

for id in uniquevalues:
    newdf = df[df['id'] == id]
    newdf.to_csv("dataframe "+id+".csv", sep='\t')

If you have only those three IDs, then you can just pass the for and do the same thing manually like

newdf = df[df['id'] == "0001"]
newdf.to_csv("dataframe0001.csv", sep='\t')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Filter DataFrame based on Max value in Column - Pandas

From Dev

Filter pandas DataFrame by column time value

From Dev

How to filter a pandas dataframe by dict column?

From Dev

Filter pandas dataframe based on a column: keep all rows if a value is that column

From Dev

Filter pandas dataframe by quantile based on the value of another column

From Dev

Filter pandas dataframe by quantile based on the value of another column

From Dev

how to shift single value of a pandas dataframe column

From Dev

how do you filter a Pandas dataframe by a multi-column set?

From Dev

How to filter column values in the pandas dataframe with certain conditions?

From Dev

Pandas - how to filter dataframe by regex comparisons on mutliple column values

From Dev

Is it possible to filter Pandas DataFrame column if column is a list?

From Dev

How do I filter a pandas DataFrame based on value counts?

From Java

Pandas split DataFrame by column value

From Dev

Pandas DataFrame column value remapping

From Dev

how to rename a column value in pandas dataframe on some condition

From Dev

How to remove string value from column in pandas dataframe

From Java

How to drop rows of Pandas DataFrame whose value in a certain column is NaN

From Dev

How to substract a single value from column of pandas DataFrame

From Dev

How to compress a pandas dataframe, based on their boolean column value?

From Dev

How to replace a value in a pandas dataframe with column name based on a condition?

From Dev

How to apply a function to every value in a column in a pandas dataframe?

From Dev

How to shift a column in Pandas DataFrame without losing value

From Dev

How to get column name for second largest row value in pandas DataFrame

From Dev

pandas dataframe: how to aggregate a subset of rows based on value of a column

From Dev

How to assign a value to a column for every row of pandas dataframe?

From Dev

How to assign a value to a column for a subset of dataframe based on a condition in Pandas?

From Dev

How to explode pandas dataframe based on number value in a specific column

From Dev

How to check in which column is certain value in pandas.DataFrame?

From Dev

How to replicate rows based on value of a column in same pandas dataframe

Related Related

  1. 1

    Filter DataFrame based on Max value in Column - Pandas

  2. 2

    Filter pandas DataFrame by column time value

  3. 3

    How to filter a pandas dataframe by dict column?

  4. 4

    Filter pandas dataframe based on a column: keep all rows if a value is that column

  5. 5

    Filter pandas dataframe by quantile based on the value of another column

  6. 6

    Filter pandas dataframe by quantile based on the value of another column

  7. 7

    how to shift single value of a pandas dataframe column

  8. 8

    how do you filter a Pandas dataframe by a multi-column set?

  9. 9

    How to filter column values in the pandas dataframe with certain conditions?

  10. 10

    Pandas - how to filter dataframe by regex comparisons on mutliple column values

  11. 11

    Is it possible to filter Pandas DataFrame column if column is a list?

  12. 12

    How do I filter a pandas DataFrame based on value counts?

  13. 13

    Pandas split DataFrame by column value

  14. 14

    Pandas DataFrame column value remapping

  15. 15

    how to rename a column value in pandas dataframe on some condition

  16. 16

    How to remove string value from column in pandas dataframe

  17. 17

    How to drop rows of Pandas DataFrame whose value in a certain column is NaN

  18. 18

    How to substract a single value from column of pandas DataFrame

  19. 19

    How to compress a pandas dataframe, based on their boolean column value?

  20. 20

    How to replace a value in a pandas dataframe with column name based on a condition?

  21. 21

    How to apply a function to every value in a column in a pandas dataframe?

  22. 22

    How to shift a column in Pandas DataFrame without losing value

  23. 23

    How to get column name for second largest row value in pandas DataFrame

  24. 24

    pandas dataframe: how to aggregate a subset of rows based on value of a column

  25. 25

    How to assign a value to a column for every row of pandas dataframe?

  26. 26

    How to assign a value to a column for a subset of dataframe based on a condition in Pandas?

  27. 27

    How to explode pandas dataframe based on number value in a specific column

  28. 28

    How to check in which column is certain value in pandas.DataFrame?

  29. 29

    How to replicate rows based on value of a column in same pandas dataframe

HotTag

Archive