girlsopk.blogg.se

Python read and write file
Python read and write file









python read and write file

In the above code, we create a DataFrame with the data using a Python dictionary. # Create a DataFrame with the data data = df = pd.DataFrame(data) # Write the DataFrame to a CSV file df.to_csv( ' output_file.csv ', index = False ) Python You can filter CSV data using Python by reading the CSV file into a pandas DataFrame and then using the various methods available in pandas to filter the data. 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.

python read and write file

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. 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











Python read and write file