convert string to lower case in pandas dataframe
- User
convert string to lower case in pandas dataframe
- Assistant
You can use the
str.lower()method of pandas to convert a string column to lowercase in a dataframe. Here is an example:import pandas as pd # Sample dataframe df = pd.DataFrame({'Name': ['John', 'Peter', 'Mary'], 'City': ['New York', 'PARIS', 'London']}) # Convert the City column to lowercase df['City'] = df['City'].str.lower() print(df)Output:
Name City 0 John new york 1 Peter paris 2 Mary londonIn this example, we are converting the
Citycolumn to lowercase using thestr.lower()method and assigning the modified column back to the same column in the dataframe. - User
rename pandas column
- Assistant
You can use the
rename()method of pandas to rename a column in a dataframe. Here's an example:import pandas as pd # Sample dataframe df = pd.DataFrame({'Name': ['John', 'Peter', 'Mary'], 'Age': [25, 30, 35]}) # Rename the Age column to Years df.rename(columns={'Age': 'Years'}, inplace=True) print(df)Output:
Name Years 0 John 25 1 Peter 30 2 Mary 35In this example, we are using the
rename()method to rename theAgecolumn toYears. We are passing the mapping of the old column name to new column name as a dictionary to thecolumnsparameter of therename()method. We are also setting theinplaceparameter toTrueto modify the dataframe in place. - User
rename only one column in pandas
- Assistant
You can pass the mapping dictionary with only the column name that you want to rename to the
columnsparameter. Here's an example:import pandas as pd # Sample dataframe df = pd.DataFrame({'Name': ['John', 'Peter', 'Mary'], 'Age': [25, 30, 35]}) # Rename the Age column to Years df.rename(columns={'Age': 'Years'}, inplace=True) print(df)Output:
Name Years 0 John 25 1 Peter 30 2 Mary 35In this example, we are using the
rename()method to rename theAgecolumn toYears. We are passing the mapping of the old column name to new column name as a dictionary to thecolumnsparameter of therename()method. We are also setting theinplaceparameter toTrueto modify the dataframe in place. - User
group by in pandas data frame
- Assistant
You can use the
groupby()method of pandas to group a dataframe by one or more columns based on a certain condition, and then apply some aggregation function to the groups to obtain a summary of the data. Here's an example of how to use thegroupby()method:import pandas as pd # Sample data data = {'Name': ['John', 'Peter', 'Mary', 'John', 'Mary'], 'Age': [25, 30, 35, 40, 45], 'Gender': ['M', 'M', 'F', 'M', 'F'], 'Salary': [50000, 60000, 70000, 80000, 90000]} df = pd.DataFrame(data) # Group by the Gender column and get the mean of Salary for each group grouped = df.groupby('Gender')['Salary'].mean() print(grouped)Output:
Gender F 80000.0 M 63333.333333 Name: Salary, dtype: float64In this example, we are using the
groupby()method to group the dataframedfby theGendercolumn, and then applying themean()function to theSalarycolumn of each group to get the average salary for each gender group. The resulting object is a series with the mean values indexed by gender group.