--Environment --macOS Catalina version 10.15.7 - Python 3.8.5 - pandas 1.1.3
df = pandas.read_csv('CSV.csv')
print(df)
output
Name Count Start End
0 Ponsuke 1 9:00 18:00
1 Ponsuke 2 18:00 NaN
2 Ponsuke 3 9:00 13:00
3 Ponsuke 4 NaN NaN
4 Ponsuke 5 9:00 NaN
5 Ponsuke 6 18:00 NaN
6 Ponsuke 7 12:00 NaN
7 Ponsuke 8 12:00 NaN
8 Ponsuke 9 NaN 18:00
9 Ponsuke 10 NaN NaN
I want to extract data |
start | End |
---|---|---|
x | NaN | NaN |
o | Not NaN | NaN |
x | NaN | Not NaN |
o | Not NaN | Not NaN |
Method
# [start]How to delete data whose column is NaN
print(df.dropna(subset=['start']))
# [start]How to extract data whose columns are not NaN
print(df[df['start'].notna()])
# [start]How to extract data whose columns are not NaN
print(df.query('start.notna()', engine='python'))
output
Name Count Start End
0 Ponsuke 1 9:00 18:00
1 Ponsuke 2 18:00 NaN
2 Ponsuke 3 9:00 13:00
4 Ponsuke 5 9:00 NaN
5 Ponsuke 6 18:00 NaN
6 Ponsuke 7 12:00 NaN
7 Ponsuke 8 12:00 NaN
I want to extract data |
start | End |
---|---|---|
x | NaN | NaN |
x | Not NaN | NaN |
x | NaN | Not NaN |
o | Not NaN | Not NaN |
Method
# [start]Or[End]How to delete data in one of the columns NaN
print(df.dropna(subset=['start', 'End']))
# [start][End]How to extract data where both columns are not NaN
print(df.query('start.notna() &End.notna()', engine='python'))
output
Name Count Start End
0 Ponsuke 1 9:00 18:00
2 Ponsuke 3 9:00 13:00
I want to extract data |
start | End |
---|---|---|
x | NaN | NaN |
o | Not NaN | NaN |
o | NaN | Not NaN |
o | Not NaN | Not NaN |
Method
#Make NaN an empty string[start][End]How to extract non-empty data by sticking columns together
print(df[df['start'].str.cat(df['End'], na_rep='') != ''])
# [start][End]How to extract data where either column is not NaN
print(df.query('start.notna() |End.notna()', engine='python'))
output
Name Count Start End
0 Ponsuke 1 9:00 18:00
1 Ponsuke 2 18:00 NaN
2 Ponsuke 3 9:00 13:00
4 Ponsuke 5 9:00 NaN
5 Ponsuke 6 18:00 NaN
6 Ponsuke 7 12:00 NaN
7 Ponsuke 8 12:00 NaN
8 Ponsuke 9 NaN 18:00
I want to extract data |
start | End |
---|---|---|
x | NaN | NaN |
x | Not NaN | NaN |
o | NaN | Not NaN |
x | Not NaN | Not NaN |
Method
# [start]After the column extracts NaN[End]How to delete data whose column is NaN
print(df[df['start'].isna()].dropna(subset=['End']))
# [start]The column is not NaN and[End]How to extract data where the column is NaN
print(df.query('start.isna() &End.notna()', engine='python'))
output
Name Count Start End
8 Ponsuke 9 NaN 18:00
Recommended Posts