You need as_index=False
and group_keys=False
:
df = df.groupby(["PlatformCategory", "Platform", "ResClassName"], as_index = False).count()
df
Empty DataFrame
Columns: [PlatformCategory, Platform, ResClassName, Amount]
Index: []
Some code that works the same for .sum()
whether or not the dataframe is empty:
def groupby_sum(df, groupby_cols):
groupby = df.groupby(groupby_cols, as_index = False)
summed = groupby.sum()
return (groupby.count() if summed.empty
else summed).set_index(groupby_cols)
df = groupby_sum(df, ["PlatformCategory", "Platform", "ResClassName"])
Keep columns after a groupby in an empty dataframe,Pandas: Groupby multiple columns, finding the max value and keep other columns in dataframe,Preserving id columns in dataframe after applying assign and groupby,Combining columns as string after groupby aggregation in Pandas dataframe
You need as_index=False
and group_keys=False
:
df = df.groupby(["PlatformCategory", "Platform", "ResClassName"], as_index = False).count()
df
Empty DataFrame
Columns: [PlatformCategory, Platform, ResClassName, Amount]
Index: []
Some code that works the same for .sum()
whether or not the dataframe is empty:
def groupby_sum(df, groupby_cols):
groupby = df.groupby(groupby_cols, as_index = False)
summed = groupby.sum()
return (groupby.count() if summed.empty
else summed).set_index(groupby_cols)
df = groupby_sum(df, ["PlatformCategory", "Platform", "ResClassName"])
The dataframe is an empty df after query.when groupby,raise runtime waring,then get another empty dataframe with no columns.How to keep the columns?, 1 week ago Dec 23, 2019 · 问题. The dataframe is an empty df after query.when groupby,raise runtime waring,then get another empty dataframe with no columns.How to keep the columns? , 6 days ago The dataframe is an empty df after query.when groupby,raise runtime waring,then get another empty dataframe with no columns.How to keep the columns? df = pd.DataFrame(columns=["PlatformCategory"," ... Keep columns after a groupby in an empty dataframe. Ask Question Asked 4 years, 10 months ago. Modified 10 months ago. , Notice that the resulting DataFrame drops the “assists” and “rebounds” columns from the original DataFrame and keeps the remaining columns.
df = pd.DataFrame(columns = ["PlatformCategory", "Platform", "ResClassName", "Amount"]) print df
df = df.groupby(["PlatformCategory", "Platform", "ResClassName"], as_index = False).count() df Empty DataFrame Columns: [PlatformCategory, Platform, ResClassName, Amount] Index: []
You can find out how to create an empty DataFrame with column names and indices and then append rows one by one to it using DataFrame.loc[] property. The loc[] property is used to access a group of rows and columns by label(s) or a boolean array.,First, let’s create an empty pandas DataFrame without any column names or indices and then append columns one by one to it.,In this article, You have learned how to append rows and columns and indices using DataFrame.append() and DataFrame.loc[] property with multiple examples.,You can find out how to create an empty pandas DataFrame and append rows and columns to it by using DataFrame.append() method and DataFrame.loc[] property. In this article, I will explain how to append a row and column to empty DataFrame by several methods.
If you are in hurry below are some quick examples to append rows and columns to an empty DataFrame in pandas.
# Below are some quick examples. # Create a empty DataFrame. df = pd.DataFrame() # Append columns to an empty DataFrame. df['Courses'] = ['Spark', 'PySpark', 'Python'] df['Fee'] = [15000, 20000, 25000] df['Duration'] = ['30days', '35days', '50days'] # Append Rows to Empty DataFrame. df2 = df.append({ 'Courses': 'Spark', 'Fee': 15000, 'Discount': '30days' }, ignore_index = True) # Create DataFrame with Column name and indices using loc[] property. df = pd.DataFrame(columns = ['Courses', 'Fee', 'Duration'], index = ['1', '2', '3']) df.loc['1'] = ['Courses', 15000, '30days'] print(df)
# Create a empty DataFrame. df = pd.DataFrame() print(df) # Append columns to an empty DataFrame. df['Courses'] = ['Spark', 'PySpark', 'Python'] df['Fee'] = [15000, 20000, 25000] df['Duration'] = ['30days', '35days', '50days']
Yields below output.
Empty DataFrame
Columns: []
Index: []
Courses Fee Duration
0 Spark 15000 30 days
1 PySpark 20000 35 days
2 Python 25000 50 days
Empty DataFrame
Columns: [Courses, Fee, Duration]
Index: []
Courses Fee Duration Discount
0 Spark 15000 NaN 30 days
You can find out how to create an empty DataFrame with column names and indices and then append rows one by one to it using DataFrame.loc[]
property. The loc[]
property is used to access a group of rows and columns by label(s) or a boolean array.
# Create DataFrame with Column name and indices using loc[] property. df = pd.DataFrame(columns = ['Courses', 'Fee', 'Duration'], index = ['1', '2', '3']) df.loc['1'] = ['Courses', 15000, '30days'] print(df)
When calling apply and the by argument produces a like-indexed (i.e. a transform) result, add group keys to index to identify pieces. By default group keys are not included when the result’s index (and column) labels match the inputs, and are included otherwise. This argument has no effect if the result produced is not like-indexed with respect to the input.,When using .apply(), use group_keys to include or exclude the group keys. The group_keys argument defaults to True (include).,Changed in version 1.5.0: Warns that group_keys will no longer be ignored when the result from apply is a like-indexed Series or DataFrame. Specify group_keys explicitly to include the group keys or not.,We can also choose to include NA in group keys or not by setting dropna parameter, the default setting is True.
>>> df = pd.DataFrame({
'Animal': ['Falcon', 'Falcon',
...'Parrot', 'Parrot'
],
...'Max Speed': [380., 370., 24., 26.]
}) >>>
df
Animal Max Speed
0 Falcon 380.0
1 Falcon 370.0
2 Parrot 24.0
3 Parrot 26.0
>>>
df.groupby(['Animal']).mean()
Max Speed
Animal
Falcon 375.0
Parrot 25.0
>>> arrays = [
['Falcon', 'Falcon', 'Parrot', 'Parrot'],
...['Captive', 'Wild', 'Captive', 'Wild']
] >>>
index = pd.MultiIndex.from_arrays(arrays, names = ('Animal', 'Type')) >>>
df = pd.DataFrame({
'Max Speed': [390., 350., 30., 20.]
},
...index = index) >>>
df
Max Speed
Animal Type
Falcon Captive 390.0
Wild 350.0
Parrot Captive 30.0
Wild 20.0
>>>
df.groupby(level = 0).mean()
Max Speed
Animal
Falcon 370.0
Parrot 25.0
>>>
df.groupby(level = "Type").mean()
Max Speed
Type
Captive 210.0
Wild 185.0
>>> l = [
[1, 2, 3],
[1, None, 4],
[2, 1, 3],
[1, 2, 2]
] >>>
df = pd.DataFrame(l, columns = ["a", "b", "c"])
>>> df.groupby(by = ["b"]).sum()
a c
b
1.0 2 3
2.0 2 5
>>> df.groupby(by = ["b"], dropna = False).sum()
a c
b
1.0 2 3
2.0 2 5
NaN 1 4
>>> l = [
["a", 12, 12],
[None, 12.3, 33.],
["b", 12.3, 123],
["a", 1, 1]
] >>>
df = pd.DataFrame(l, columns = ["a", "b", "c"])
Dataframe class provides a constructor to create Dataframe object by passing column names , index names & data in argument like this, ,We can create a complete empty dataframe by just calling the Dataframe class constructor without any arguments like this, ,In this article we will discuss different ways to create an empty DataFrame and then fill data in it later by either adding rows or columns.,It might be possible in some cases that we know the column names & row indices at start but we don’t have data yet. So we will create an empty DataFrame and add data to it at later stages like this,
Import python’s pandas module like this,
import pandas as pd
Suppose we know the column names of our DataFrame but we don’t have any data as of now. So we will create an empty DataFrame with only column names like this,
# Creating an empty Dataframe with column names only dfObj = pd.DataFrame(columns = ['User_ID', 'UserName', 'Action']) print("Empty Dataframe ", dfObj, sep = '\n')
Columns: [User_ID, UserName, Action]
Index: []
As we have created an empty DataFrame, so let’s see how to add rows to it,
# Append rows in Empty Dataframe by adding dictionaries dfObj = dfObj.append({ 'User_ID': 23, 'UserName': 'Riti', 'Action': 'Login' }, ignore_index = True) dfObj = dfObj.append({ 'User_ID': 24, 'UserName': 'Aadi', 'Action': 'Logout' }, ignore_index = True) dfObj = dfObj.append({ 'User_ID': 25, 'UserName': 'Jack', 'Action': 'Login' }, ignore_index = True) print("Dataframe Contens ", dfObj, sep = '\n')
We can create a complete empty dataframe by just calling the Dataframe class constructor without any arguments like this,
# Create an completely empty Dataframe without any column names, indices or data dfObj = pd.DataFrame()