提取每个分组/ID 中特定列中的自然数。

huangapple go评论66阅读模式
英文:

Extract only natural numbers belonging to a specific column, for every group/id in Python

问题

# 加载所需的库
import pandas as pd

# 创建数据集
data = {'id': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
               2, 2, 2, 2, 2, 2, 2,
               3, 3, 3, 3, 3, 3, 3, 3,
               4, 4, 4, 4, 4, 4,
               5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
        'cycle': [0.0, 0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2,
                  0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2,
                  0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4,
                  0.0, 0.2, 0.4, 0.6, 0.8, 1.0,
                  0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0, 3.2],
        'Salary': [6, 7, 7, 7, 8, 9, 10, 11, 12, 13, 14, 15,
                   3, 4, 4, 4, 4, 5, 6,
                   2, 8, 9, 10, 11, 12, 13, 14,
                   1, 8, 9, 10, 11, 12,
                   6, 7, 7, 9, 10, 11, 12, 13, 14, 15, 9, 10, 11, 12, 13, 14, 15],
        'Children': ['Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'No', 'No', 'Yes', 'Yes', 'Yes', 'No',
                     'Yes', 'Yes', 'Yes', 'No', 'Yes', 'Yes', 'Yes',
                     'Yes', 'No', 'Yes', 'Yes', 'No', 'No', 'Yes', 'Yes',
                     'Yes', 'Yes', 'No', 'Yes', 'Yes', 'Yes',
                     'Yes', 'No', 'Yes', 'No', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'No', 'No', 'Yes', 'No', 'No', 'Yes', 'Yes', 'Yes'],
        'Days': [141, 123, 128, 66, 66, 120, 141, 52, 96, 120, 141, 52,
                 141, 96, 120, 120, 141, 52, 96,
                 141, 15, 123, 128, 66, 120, 141, 141,
                 141, 141, 123, 128, 66, 67,
                 141, 123, 128, 66, 123, 128, 66, 120, 141, 52, 123, 128, 66, 123, 128, 66, 120]
        }

# 转换为数据框
df = pd.DataFrame(data)
print("\n df = \n", df)

以上是你提供的代码部分的翻译。如果你有关于代码的具体问题或需要进一步的解释,请随时提出。

英文:
#Load the required libraries
import pandas as pd
#Create dataset
data = {'id': [1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3,3,
4, 4, 4, 4, 4,4,
5, 5, 5, 5, 5, 5,5, 5, 5,5,     5,5, 5,5, 5, 5,5],
'cycle': [0.0, 0.2,0.4, 0.6, 0.8, 1,1.2,1.4,1.6,1.8,2.0,2.2,
0.0, 0.2,0.4, 0.6,0.8,1.0,1.2,
0.0, 0.2,0.4, 0.6, 0.8,1.0,1.2,1.4,
0.0, 0.2,0.4, 0.6, 0.8,1.0,
0.0, 0.2,0.4, 0.6, 0.8, 1.0,1.2,1.4,1.6,1.8,   2.0,2.2,2.4,2.6,2.8,3.0,3.2],
'Salary': [6, 7, 7, 7,8,9,10,11,12,13,14,15,
3, 4, 4, 4,4,5,6,
2, 8,9,10,11,12,13,14,
1, 8,9,10,11,12,
6, 7, 7,9,10,11,12,13,14,15, 9,10,11,12,13,14,15],
'Children': ['Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'No','No', 'Yes', 'Yes', 'Yes', 'No',
'Yes', 'Yes', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 
'Yes', 'No','Yes', 'Yes', 'No','No', 'Yes','Yes',
'Yes', 'Yes', 'No','Yes', 'Yes','Yes',
'Yes', 'No',  'Yes', 'No', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'No',    'No',  'Yes', 'No', 'No', 'Yes', 'Yes', 'Yes'],
'Days': [141, 123, 128, 66, 66, 120, 141, 52,96, 120, 141, 52,
141, 96, 120,120, 141, 52,96,
141,  15,123, 128, 66, 120, 141, 141,
141, 141,123, 128, 66,67,
141, 123, 128, 66, 123, 128, 66, 120, 141, 52,   123, 128, 66, 123, 128, 66, 120,],
}
#Convert to dataframe
df = pd.DataFrame(data)
print("\n df = \n",df)

The above dataset looks as such:

提取每个分组/ID 中特定列中的自然数。

Here for every 'id', I wish to extract only those rows, whose 'cycle' is an natural number, as shown in the boxes of the above image file.

The expected dataframe is as such:

提取每个分组/ID 中特定列中的自然数。

Can somebody please let me know how to achieve this task in Pyhton?

答案1

得分: 1

筛选出不等于0的值,并比较整数值是否与浮点数值相同:

out = df[df['cycle'].ne(0) & df['cycle'].astype(int).eq(df['cycle'])].reset_index(drop=True)
print(out)
   id  cycle  Salary Children  Days
0   1    1.0       9      Yes   120
1   2    1.0       5      Yes    52
2   3    1.0      12       No   120
3   4    1.0      12      Yes    67
4   5    1.0      11      Yes   128
5   5    2.0       9       No   123
6   5    3.0      14      Yes    66
英文:

Filter out 0 values and compare if integer values is same like float value:

out=df[df['cycle'].ne(0) & df['cycle'].astype(int).eq(df['cycle'])].reset_index(drop=True)
print (out)
id  cycle  Salary Children  Days
0   1    1.0       9      Yes   120
1   1    2.0      14      Yes   141
2   2    1.0       5      Yes    52
3   3    1.0      12       No   120
4   4    1.0      12      Yes    67
5   5    1.0      11      Yes   128
6   5    2.0       9       No   123
7   5    3.0      14      Yes    66

答案2

得分: 1

这是另一种方法:

df[(df['cycle']!=0.0) & (df['cycle']%0.5==0)].reset_index()
英文:

Here's another way:

df[(df['cycle']!=0.0) & (df['cycle']%0.5==0)].reset_index()

huangapple
  • 本文由 发表于 2023年6月15日 14:40:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定