How to do DATA Wrangling in a Pandas DataFrame in Python
In this Learn through Codes example, you will learn: How to do DATA Wrangling in a Pandas DataFrame in Python.
The code you provided is a Python script that demonstrates how to use the Pandas library to do data wrangling on a DataFrame and a Series. The Pandas library is a powerful tool for data analysis and manipulation in Python.
The script starts by importing the Pandas library and turning off warning messages.
First, it demonstrates how to work with a Pandas Series, which is a one-dimensional array-like data structure. The script creates a series called ‘floodingReports’ using the pd.Series() function and assigns the values [5, 6, 2, 9, 12].
Next, it sets county names as the index of the series using the index parameter. It then prints the series and demonstrates how to access the value of a specific element in the series using its index. After that it filters the series by using a comparison operator on the series, so that it prints only those elements which are greater than 6.
Then it creates a dictionary called ‘fireReports_dict’ with county names as keys and number of fire reports as values, then it converts that dictionary into a pd.Series, and changes its index to shorter names.
After that, it demonstrates how to work with a Pandas DataFrame. A DataFrame is a two-dimensional table-like data structure that can hold multiple data types and can be used to store and manipulate data. The script creates a DataFrame ‘df’ from a dictionary containing equal-length lists. Then it prints the DataFrame.
Then it demonstrates how to set the order of columns using the columns attribute. It creates a new DataFrame ‘dfColumnOrdered’ using the same data, but with the column order specified in the columns parameter. After that, it adds a new column to the DataFrame and then deletes that column.
Finally, it demonstrates how to transpose the DataFrame using the T attribute. This will switch the rows and columns of the DataFrame.
In conclusion, the script demonstrates how to use the Pandas library to do data wrangling on a DataFrame and a Series. The script uses several functions and attributes of the Pandas library to load, clean, and transform data, making it more suitable for analysis. It shows how to use Pandas Dataframe and Series to load, clean and manipulate data efficiently, which is very useful in data science, analysis and machine learning projects.