How to filter values by Column Name and then extract the rows that have the same value to another CSV file? Python/Pandas

AlpU

I have a pandas DataFrame with 4 columns, the first being "ID NUMBER". I am trying to filter "ID NUMBER" and get the same values bundled together. After that I want to extract each one that have the same values to a different csv file with their respected name.

DataFrame:

     ID Number    col2           col3     DATE
0   111            0.5          -0.6    20160104
1   118           -0.1          -0.6    20160104
2   11D            0.3          -1.1    20160104
3   111           -0.7          -0.9    20150102


 ***Output I need:***
 Number ID    col2           col3     DATE
0   111            0.5          -0.6    20160104
1   111           -0.7          -0.9    20150102

I have attempted to do something, however I could not find anything about how to filter a columns, and then extract online. Thank you!

EdChum

You can use duplicated with param keep=False so it returns True for all duplicated rows and mask the df:

In [16]:
df[df['ID Number'].duplicated(keep=False)]

Out[16]:
  ID Number  col2  col3      DATE
0       111   0.5  -0.6  20160104
3       111  -0.7  -0.9  20150102

For the second part you can do:

gp = df[df['ID Number'].duplicated(keep=False)].groupby('ID Number')
gp.apply(lambda x: x.to_csv(str(x.name) + '.csv')

EDIT

Actually if you're just wanting to write all rows with the same ID number to a named csv then:

df.groupby('ID Number').apply(lambda x: x.to_csv(str(x.name) + '.csv'))

Should do what you want

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to extract all rows that have the same value in a column as one of the rows?

extract values of column according to value in another columns in csv file

Pandas - filter rows with same value in one column and multiple values in another column based on the existence of a value in the latter column

Formula to check if rows that have the same value in one column have matching values in another column

How to update the value in a column based on another value in the same column where both rows have the same value in another column?

Find rows that have same value in one column and other values in another column?

Rows that have same value in a column, sum all values in another column and display 1 row

Pyspark: How to set the same id to all the rows that have the same value in another column?

Find rows that have same values in another column - Python

Select rows with the same value in one column and specific values in another column

How can I get the this ruby script to output the same rows and columns as another CSV file and add another column to it?

How to fetch Rows have not this column value in another table column value

Filter DataFrame if group of indexes have the same value in another column

How to select the rows having same id and have all missing value in another column

How to sum rows of two or more csv files that have the same value in column 1?

How do I create a new column that holds all the primary key values of all rows that have the same value?

How can I select rows with all Column value, which have the same values?

For rows that have repeated column values, how to create another row with a unique count number for that value

How to remove rows that have all NaN values for a specific value in another column?

How to count rows of a column by depending on same values of another column?

Delete rows that have duplicate column value in CSV file

how to delete rows from a csv file which string in 1st column is the same of string in 1st column of another csv?

How to iterate over a CSV file and update values in one column based on the value of another column

How to select rows that have certain values present in another column

Extract rows from a CSV file that contain any of the values from another CSV file

is it possible for two rows of a column (primary key) of the table to have same character/value with same foreign key(another column)

Pandas: Filter rows by comparing a column's value to another value for the same column in a different row

How to filter rows based on comparing another column's value

For each value of a column divide that value by the number of rows that have the same sequence in another column (counting multimapped reads with R)