如何按日期在“熊猫”列中排序?

时间:2018-12-05 21:22:51

标签: python pandas

我有熊猫数据

df = pd.DataFrame({
'country': ['USA', 'USA', 'Belarus', 'USA'],
'population': [17.04, 143.5, 9.5, 45.5],
'square': [2724902, 17125191, 207600, 603628],
'time with timestamp': ['2018-12-03 16:02:06.948083+00:00', '2018-12-03 16:10:25.625414+00:00', '2018-12-03 15:57:54.850205+00:00', '2018-12-03 15:57:54.850205+00:00']
})
df2 = data[data['country'] == 'USA']['country', 'time with timestamp']
print(df2)
  1. 为什么按国家/地区过滤不起作用?
  2. 我是否有某种方法可以按数据对所有列进行排序? 我的目标:按名称USA获取所有列+按时间顺序对所有列进行排序 祝你好运和新的胜利! 附言解决方案

`

df = pd.DataFrame({
'country': ['USA', 'USA', 'Belarus', 'USA'],
'population': [17.04, 143.5, 9.5, 45.5],
'square': [2724902, 17125191, 207600, 603628],
'time with timestamp': ['2018-12-03 16:02:06.948083+00:00', '2018-12-03 16:10:25.625414+00:00', '2018-12-03 15:57:54.850205+00:00', '2018-12-03 15:57:54.850205+00:00']
})
df2 = df.loc[df['country'] == 'USA', ['country', 'time with timestamp']]
df3 = df2.sort_values("time with timestamp")
print(df3)
print(df3.loc[:, ['country', 'time with timestamp']])

`

0 个答案:

没有答案