9951 explained code solutions for 126 technologies


python-pandasHow to get single row from dataframe by column value


import pandas as pd

data = pd.DataFrame({
  'Vendor': ['US', 'US', 'US', 'KR', 'KR'],
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Price': [204, 304, 102, 405, 350],
  'Used': [150, 250, 80, 320, 280]
})

row = data[data['Phone'] == 'ip8'].iloc[0]ctrl + c
import pandas as pd

load Pandas module

pd.DataFrame

creates Pandas DataFrame object

'Phone'

column name to filter by

ip8

column value to find rows by

.iloc[0]

will return first row from filtered dataframe

row

will contain single found row


Usage example

import pandas as pd

data = pd.DataFrame({
  'Vendor': ['US', 'US', 'US', 'KR', 'KR'],
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Price': [204, 304, 102, 405, 350],
  'Used': [150, 250, 80, 320, 280]
})

row = data[data['Phone'] == 'ip8'].iloc[0]
print(row)
output
Vendor     US
Phone     ip8
Price     102
Used       80
Name: 2, dtype: object