简体   繁体   English

如何按列名过滤值,然后将具有相同值的行提取到另一个CSV文件? Python /熊猫

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

I have a pandas DataFrame with 4 columns, the first being "ID NUMBER". 我有一个带有4列的pandas DataFrame,第一列是“ ID NUMBER”。 I am trying to filter "ID NUMBER" and get the same values bundled together. 我正在尝试过滤“ ID NUMBER”并将相同的值捆绑在一起。 After that I want to extract each one that have the same values to a different csv file with their respected name. 之后,我想将每个具有相同值的名称提取到具有相应名称的另一个csv文件中。

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! 谢谢!

You can use duplicated with param keep=False so it returns True for all duplicated rows and mask the df: 您可以将duplicated与param keep=False一起使用,以便为所有重复的行返回True并屏蔽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: 实际上,如果您只想将具有相同ID号的所有行写入命名的csv,则:

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

Should do what you want 应该做你想做的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为在另一列 pandas 中具有相同值的那些行使一列的值相同 - How to make same value of one column for those rows which have same values in another column pandas Python Pandas - 过滤 pandas dataframe 以获取一列中具有最小值的行,以获取另一列中的每个唯一值 - Python Pandas - filter pandas dataframe to get rows with minimum values in one column for each unique value in another column How to read every column of a csv file in python after every 10-15 rows which have the same header using pandas or csv? - How to read every column of a csv file in python after every 10-15 rows which have the same header using pandas or csv? 连接Python中具有相同第一列值的CSV文件的所有行 - Joining all rows of a CSV file that have the same 1st column value in Python 用python连接具有相同第一列的csv文件的所有行 - joining all rows of a csv file that have the same first column with python 在另一列中查找具有相同值的行 - Python - Find rows that have same values in another column - Python 如何组合 pandas dataframe 中在一列中具有相同值的行 - How to combine rows in a pandas dataframe that have the same value in one column 如何检查 pandas 列中接下来的 3 个连续行是否具有相同的值? - How to check if next 3 consecutive rows in pandas column have same value? 如何使用python(pandas)更新csv文件中所有行的最后一列值 - How to update the last column value in all the rows in csv file using python(pandas) 如何将新值附加/更新到现有 csv 文件的行中,从新的 csv 文件作为 python 或其他内容的新列 - How to append/update new values to the rows of a existing csv file from a new csv file as a new column in python using pandas or something else
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM