Python Pandas按一列分组

时间:2018-09-12 01:17:33

标签: python pandas pandas-groupby

假设我在Python中有这样的数据框;

  <label>Are you sure?
      <input type="checkbox">
  </label>

如何通过varX分组此数据帧并取第三列的总和(varZ),以便我们可以创建一个如下所示的新数据帧;

#flutter specific
.flutter-plugins
.DS_Store
.dart_tool/
.packages
.pub/
build/
android/app/google-services.json
.idea/
android/key.properties
pubspec.lock
doc/api/

#ios specific
ios/.generated/
ios/Podfile*
ios/Flutter/Debug.xcconfig
ios/Flutter/Release.xcconfig
ios/Runner.xcworkspace/contents.xcworkspacedata

#android specific
android/app/google-services.json
android/key.properties

# User-specific
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
.idea/modules.xml
.idea/*.iml
.idea/modules
*.iml

# File-based project format
*.iws

# IntelliJ
out/

我已经尝试了以下方法,但是没有用。

varX varY varZ
01   A     34
01   B     26
02   A     18
02   B     50

非常感谢您的帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

尝试对sum列执行varZ

print(df.groupby("varX")['varZ'].sum())

您想做什么as_index=False

print(df.groupby("varX",as_index=False)['varZ'].sum())