Python Pandas Find Starting/ending Row And Rounding Numbers
import pandas as pd import numpy as np import urllib url = 'http://cawcr.gov.au/staff/mwheeler/maproom/RMM/RMM1RMM2.74toRealtime.txt' urllib.urlretrieve(url,'datafile.txt') df =
Solution 1:
To quickly answer your last question, use:
import numpy as np
np.round(ec, 0)
For your first (series of) question(s), you don't give us any data to play with and your questions isn't very clear. Either way, you can always get the the first and last rows of any dataframe with df.iloc[0]
and df.iloc[-1]
, respectively.
Edits:
If you simply need to know how many rows you have, use df.shape
. Here's a toy example:
import pandas
df = pandas.DataFrame([
(1977, 1, 1),
(1978, 1, 2),
(1979, 1, 3),
(1980, 1, 4),
(1977, 2, 1),
(1978, 2, 2),
(1979, 2, 3),
(1980, 2, 4),
(1977, 3, 1),
(1978, 3, 2),
(1979, 3, 3),
(1980, 3, 4),
], columns=['year', 'a', 'b'])
print(df.to_string())
Which prints:
yearab01977 1111978 1221979 1331980 1441977 2151978 2261979 2371980 2481977 3191978 32101979 33111980 34
And then:
df = df[df.year > 1978]
df = df[df.a < 3]
print(df.to_string())
which gives:
yearab21979 1331980 1461979 2371980 24
Try this our yourself after executing everything above:
print(df.shape)
for row in range(df.shape[0]-1):
print(df.iloc[row])
For rounding:
df = pandas.DataFrame(np.random.normal(size=(4,4)))
rounded = np.round(df,1)
print(rounded.to_string())
0 1 2 3
0 -1.2 1.9 0.7 -0.8
1 -0.5 0.9 1.6 -0.3
2 0.4 -0.2 -1.6 -0.2
3 -1.7 1.1 0.1 -0.6
Post a Comment for "Python Pandas Find Starting/ending Row And Rounding Numbers"