如何找到对应的值python

时间:2017-12-13 16:18:35

标签: python pandas csv

所以我的值看起来像这样(在csv文件中):

   162,100522,19010101,  -31,    0

   162,100522,19010102,  -13,    0

   162,100522,19010103,   -5,    0

   162,100522,19010104,  -10,    0

   162,100522,19010105,  -18,    0


   162,100522,19010106,  -78,    0

   162,100522,19010107,  -66,    0

   162,100522,19010108,   -6,    0

   162,100522,19010109,   42,    0

19010101年是1901年1月1日

和-31将是最高或最低温度。

我需要找到最高和最低温度的日期

我试过

data["DATE"][data["TX"].max()]

但它没有用......我知道我可以找到最大数据[“TX”]。max()和min与数据[“TX”]。min(),但我如何找到相应的日期最高和最低温度?也许有枚举?

2 个答案:

答案 0 :(得分:2)

试试这样:

min_temp = data.loc[data['TX'].idxmin(), 'DATE']
max_temp = data.loc[data['TX'].idxmax(), 'DATE']

答案 1 :(得分:0)

以下是一个稍微不同的实现示例,并将日期转换为datetime对象。

In [50]: import pandas as pd
In [51]: import io
In [52]: fp = io.StringIO('''   162,100522,19010101,  -31,    0
...:
...:    162,100522,19010102,  -13,    0
...:
...:    162,100522,19010103,   -5,    0
...:
...:    162,100522,19010104,  -10,    0
...:
...:    162,100522,19010105,  -18,    0
...:
...:
...:    162,100522,19010106,  -78,    0
...:
...:    162,100522,19010107,  -66,    0
...:
...:    162,100522,19010108,   -6,    0
...:
...:    162,100522,19010109,   42,    0''')

In [53]: df = pd.read_csv(fp, skipinitialspace=True, names=['A','B','Date','TX','C'])
In [54]: df['DateObj'] = pd.to_datetime(df.Date, format='%Y%m%d')
In [55]: df['DateObj'][df.TX == df.TX.min()]
Out[55]:
5   1901-01-06
Name: DateObj, dtype: datetime64[ns]
In [56]: df['DateObj'][df.TX == df.TX.max()]
Out[56]:
8   1901-01-09
Name: DateObj, dtype: datetime64[ns]
In [57]: df['Date'][df.TX == df.TX.min()]
Out[57]:
5    19010106
Name: Date, dtype: int64
In [58]: df['Date'][df.TX == df.TX.max()]
Out[58]:
8    19010109
Name: Date, dtype: int64