Skip to content Skip to sidebar Skip to footer

Grouper And Axis Must Be Same Length In Python

I am a beginner of Python, and I study a textbook to learn the Pandas module. I have a dataframe called Berri_bike, and it is from the following code: bike_df=pd.read_csv(os.path

Solution 1:

You copy your column into a pandas series instead of a new dataframe, hence the following operations behave differently. You can see this if you print out Berri_bike because it doesn't show the column name.
Instead, you should copy the column into a new dataframe:

import pandas as pd
df = pd.DataFrame(np.random.randint(0, 30, size = (70, 2)),
                  columns = ["A", "B"],
                  index = pd.date_range("20180101", periods = 70))

Berri_bike = df[["A"]]

Berri_bike['Weekday'] = Berri_bike.index.weekday

weekday_counts = Berri_bike.groupby("Weekday").sum()
print(weekday_counts)
#sample output
          A
Weekday    
0        148
1        101
2        127
3        139
4        163
5         74
6        135

Post a Comment for "Grouper And Axis Must Be Same Length In Python"