在Python中重置索引

时间:2019-12-21 13:34:43

标签: python pandas

从一个大约3400的数据帧中,询问很简单,我选择了一个特定的列:

def create_dict(row):
    return ({"userid" : row.userid ,"data": {"url": row.url},"bool":True})

输出:

df1 = df.loc[df["date"] == "2019-12-19 09:31:00+05:30"]

现在我需要的是

       date                       open   high    low    close   volume
16  2019-12-19 09:31:00+05:30   32171.5 32171.5 32138.8 32149.0 39420

为此我尝试了

a = 32171.5

这样我得到了期望的结果,但是我想要的是能够调用索引为0的值,这样我就可以使代码更具动态性,因为今天它出现在索引16中,以后可能是索引4。 / p>

我也不能使用任何其他信息,例如Date,Open或任何其他列,因为它们都是动态的,所以列名是我唯一的选择。

3 个答案:

答案 0 :(得分:1)

将索引从0更改为len(df)。     将numpy导入为np

df1 = df.loc[df["date"] == "2019-12-19 09:31:00+05:30"]
df1.index = np.arange(0,len(df1))
print(df1)

输出:

      date                       open   high    low    close   volume
0  2019-12-19 09:31:00+05:30   32171.5 32171.5 32138.8 32149.0 39420

答案 1 :(得分:0)

我认为解决方案在您的问题标题中,您可以使用:

df1.reset_index(inplace=True, drop=True)

要将索引重置为0。

答案 2 :(得分:0)

由于I/flutter (22910): [2019-12-21 13:34:09.526343] Hello, world! isolate=957159586 function='Closure: () => void from Function 'printHello': static.' E/flutter (22910): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences) E/flutter (22910): #0 getData (package:my_jungle_2/main.dart:34:41) E/flutter (22910): <asynchronous suspension> E/flutter (22910): #1 printHello (package:my_jungle_2/main.dart:28:23) E/flutter (22910): #2 _alarmManagerCallbackDispatcher.<anonymous closure> (package:android_alarm_manager/android_alarm_manager.dart:43:14) E/flutter (22910): #3 MethodChannel._handleAsMethodCall (package:flutter/src/services/platform_channel.dart:402:55) E/flutter (22910): #4 MethodChannel.setMethodCallHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:370:54) E/flutter (22910): #5 _DefaultBinaryMessenger.handlePlatformMessage (package:flutter/src/services/binding.dart:200:33) E/flutter (22910): #6 _invoke3.<anonymous closure> (dart:ui/hooks.dart:303:15) E/flutter (22910): #7 _rootRun (dart:async/zone.dart:1126:13) E/flutter (22910): #8 _CustomZone.run (dart:async/zone.dart:1023:19) E/flutter (22910): #9 _CustomZone.runGuarded (dart:async/zone.dart:925:7) E/flutter (22910): #10 _invoke3 (dart:ui/hooks.dart:302:10) E/flutter (22910): #11 _dispatchPlatformMessage (dart:ui/hooks.dart:162:5) 仅具有一行,因此您可以简单地获取第一行(唯一)并获取单元格值:

df1