How to filter CSV data using Pythonįilter the data based on your criteria. The index=False parameter specifies that we do not want to write the row index to the CSV file. The df] statement selects the ‘Name’ and ‘Age’ columns by name, while the df.iloc] statement selects the first and third columns (i.e., ‘Name’ and ‘Salary’) by index.Īfter selecting the desired columns, we export the resulting DataFrame to a new CSV file named ‘selected_data.csv’ using the to_csv() function. We then select specific columns from the DataFrame df using their names or indices. In this example, we first read a CSV file named ‘data.csv’ into a DataFrame df using the read_csv() function. Terse Mode and Json Lines Mode - one object per line with no commas separating objects.Import pandas as pd # Read the CSV file into a DataFrame df = pd.read_csv( ' data.csv ' ) # Select specific columns by name selected_cols = df] # Select specific columns by index selected_cols = df.iloc] # Export the selected columns to a new CSV file selected_cols.to_csv( ' selected_data.csv ', index = False ) Python.Optionally do not write out field : value if field value is empty.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |