Code Sample, a copy-pastable example if possible
import pandas as pd
df = pd.DataFrame({'a': [1, 9, 8, 11, -1, 6, 4, 9, 0],
'b': [9, 1, 2, 5, 6, 0, 2, 3, 1],
'c': [2, 0, 19, 1, 3, 2, 3, 2, 3]})
dummy = df.nsmallest(n = 1, columns=df.columns)
print(dummy)
Problem description
In the code above, the result should give -1, 0, 0 and not -1, 4, 3 as we're asking for the smallest number column-wise. Right now it returns the row with the minimun only fot the first column.
Expected Output
a b c
-1 0 0
Output of pd.show_versions()
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.2.final.0
python-bits: 64
OS: Linux
OS-release: 4.13.5-1-ARCH
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: es_ES.utf8
LOCALE: es_ES.UTF-8
pandas: 0.20.3
pytest: 3.2.1
pip: 9.0.1
setuptools: 36.4.0
Cython: 0.26
numpy: 1.13.1
scipy: 0.19.1
xarray: None
IPython: 6.1.0
sphinx: 1.5.5
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.2
feather: None
matplotlib: 2.0.2
openpyxl: 2.4.8
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 0.9.8
lxml: 3.8.0
bs4: 4.6.0
html5lib: 0.9999999
sqlalchemy: 1.1.13
pymysql: 0.7.9.None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: 0.5.0
**Comment From: bobhaffner**
I don't believe [DataFrame.nsmallest](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.nsmallest.html) will provide the behavior you're looking for. The `column` arg is **Column name or names to order by**
To illustrate
dummy = df.nsmallest(n = 1, columns=['c','a','b'])
print(dummy)
a b c
1 9 1 0
Maybe `df.min()` is what you're after?
**Comment From: daviddoji**
Thanks for your answer @bobhaffner. Obviously the nsmallest method is not doing what I need. The example above was just to illustrate the actual behaviour.
Imagine now that you need to take the mean value between the 4 smallest numbers for each column of a Dataframe, e.g.
df.nsmallest(n = 4, columns=df.columns).mean()
The result given by nsmallest will not give the proper value unless, as you well say, other strategy is used.
**Comment From: jreback**
@daviddoji
This is correct, nsmallest is the smallest in the column ordering breaking ties, you prob want ``df.min()`` as @bobhaffner indicates
In [7]: df.nsmallest(1, columns=df.columns)
Out[7]:
a b c
4 -1 6 3
In [8]: df
Out[8]:
a b c
0 1 9 2
1 9 1 0
2 8 2 19
3 11 5 1
4 -1 6 3
5 6 0 2
6 4 2 3
7 9 3 2
8 0 1 3
In [9]: df.min()
Out[9]:
a -1
b 0
c 0
dtype: int64