You could use either pandas.DataFrame.loc or pandas.DataFrame.iloc. See examples below.
import pandas as pd
d = [{'a': 1, 'b': 2, 'c': 3, 'd': 4},
{'a': 100, 'b': 200, 'c': 300, 'd': 400},
{'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 },
{'a': 1500, 'b': 2500, 'c': 3500, 'd': 4500}]
df = pd.DataFrame(d)
print(df) # Print original dataframe
print(df.loc[1:2]) # Print rows with index 1 and 2, (method 1)
print(df.iloc[1:3]) # Print rows with index 1 and 2, (method 2)
Original dataframe: print(df) will print:
a b c d
0 1 2 3 4
1 100 200 300 400
2 1000 2000 3000 4000
3 1500 2500 3500 4500
And print(df.loc[1:2]) for index selection by label:
a b c d
1 100 200 300 400
2 1000 2000 3000 4000
And print(df.iloc[1:3]) for row selection by integer. As mentioned by ALollz, rows are treated as numbers from 0 to len(df):
a b c d
1 100 200 300 400
2 1000 2000 3000 4000
A rule of thumb could be:
Use
.locwhen you want to refer to the actual value of the index, being a string or integer.Use
.ilocwhen you want to refer to the underlying row number which always ranges from 0 tolen(df).
Note that the end value of the slice in .loc is included. This is not the case for .iloc, and for Python slices in general.
Pandas in general
Pandas has 'easy' ways of doing all sorts of stuff like this. If you have a problem that you think is common for manipulation of tabular data, try searching for pandas ways of getting it done before inventing it yourself. Pandas will almost always have a syntactically concise and computationally faster way of doing things than what we can write ourselves.
Answer from Tim Skov Jacobsen on Stack OverflowYou could use either pandas.DataFrame.loc or pandas.DataFrame.iloc. See examples below.
import pandas as pd
d = [{'a': 1, 'b': 2, 'c': 3, 'd': 4},
{'a': 100, 'b': 200, 'c': 300, 'd': 400},
{'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 },
{'a': 1500, 'b': 2500, 'c': 3500, 'd': 4500}]
df = pd.DataFrame(d)
print(df) # Print original dataframe
print(df.loc[1:2]) # Print rows with index 1 and 2, (method 1)
print(df.iloc[1:3]) # Print rows with index 1 and 2, (method 2)
Original dataframe: print(df) will print:
a b c d
0 1 2 3 4
1 100 200 300 400
2 1000 2000 3000 4000
3 1500 2500 3500 4500
And print(df.loc[1:2]) for index selection by label:
a b c d
1 100 200 300 400
2 1000 2000 3000 4000
And print(df.iloc[1:3]) for row selection by integer. As mentioned by ALollz, rows are treated as numbers from 0 to len(df):
a b c d
1 100 200 300 400
2 1000 2000 3000 4000
A rule of thumb could be:
Use
.locwhen you want to refer to the actual value of the index, being a string or integer.Use
.ilocwhen you want to refer to the underlying row number which always ranges from 0 tolen(df).
Note that the end value of the slice in .loc is included. This is not the case for .iloc, and for Python slices in general.
Pandas in general
Pandas has 'easy' ways of doing all sorts of stuff like this. If you have a problem that you think is common for manipulation of tabular data, try searching for pandas ways of getting it done before inventing it yourself. Pandas will almost always have a syntactically concise and computationally faster way of doing things than what we can write ourselves.
Use this:
rowData = your_df.loc[ 'index' , : ]
for index, name in enumerate(df['name']):
if "good" in name or "bad" in value:
df.drop([index], inplace=True)Hi, am trying to drop rows based on this condition "if "good" in name or "bad" in value", it works this way but is this the correct way, or is there any better way to select something based on a "if-else condition"
Thanks