Home > Blockchain >  Pandas Create Dataframe
Pandas Create Dataframe

Time:11-14

I want to create new dataframe with using pandas. The table has value name and how many row that value appear

In SQL I can create the table that I want like this

SELECT start_station_name, COUNT(*) as total_number
FROM table
GROUP BY start_station_name;

But when I used Pandas with assign function I tried in this way

casual_station_name = pd.DataFrame().assign(station_name = casual_filter['start_station_name'], total_ride = casual_filter['start_station_name'].value_counts() )

But I can not do what I want

CodePudding user response:

You can achieve that in pandas dataframe by using groupby method:

casual_station_name = df.groupby(['start_station_name'])['start_station_name'].count()

CodePudding user response:

You can directy pass the data (as a dictionary) while creating the dataFrame:

casual_station_name = pd.DataFrame(dict(station_name = casual_filter['start_station_name'], total_ride = casual_filter['start_station_name'].value_counts()))

Source:

https://www.geeksforgeeks.org/different-ways-to-create-pandas-dataframe/

  • Related