python - Apply a function to a specific row using the index value -
i have following table:
import pandas pd import numpy np #dataframe random numbers , a,b,c,d,e index df = pd.dataframe(np.random.randn(5,5), index = ['a','b','c','d','e']) #now name columns same df.columns = ['a','b','c','d','e'] #resulting dataframe: b c d e 2.214229 1.621352 0.083113 0.818191 -0.900224 b -0.612560 -0.028039 -0.392266 0.439679 1.596251 c 1.378928 -0.309353 -0.651817 1.499517 0.515772 d -0.061682 1.141558 -0.811471 0.242874 0.345159 e -0.714760 -0.172082 0.205638 0.220528 1.182013
how can apply function dataframes index? want round numbers every column index "c".
#numbers round 2 decimals: b c d e c 1.378928 -0.309353 -0.651817 1.499517 0.515772
what best way this?
for label based indexing use loc
:
in [22]: df = pd.dataframe(np.random.randn(5,5), index = ['a','b','c','d','e']) #now name columns same df.columns = ['a','b','c','d','e'] df out[22]: b c d e -0.051366 1.856373 -0.224172 -0.005668 0.986908 b -1.121298 -1.018863 2.328420 -0.117501 -0.231463 c 2.241418 -0.838571 -0.551222 0.662890 -1.234716 d 0.275063 0.295788 0.689171 0.227742 0.091928 e 0.269730 0.326156 0.210443 -0.494634 -0.489698 in [23]: df.loc['c'] = np.round(df.loc['c'],decimals=2) df out[23]: b c d e -0.051366 1.856373 -0.224172 -0.005668 0.986908 b -1.121298 -1.018863 2.328420 -0.117501 -0.231463 c 2.240000 -0.840000 -0.550000 0.660000 -1.230000 d 0.275063 0.295788 0.689171 0.227742 0.091928 e 0.269730 0.326156 0.210443 -0.494634 -0.489698
Comments
Post a Comment