Can use the usecols
Using tabulate for display
import pandas as pd df = pd.read_csv('test.csv', usecols=[0,1,2,4,6]) print(df)Output
Output: Age Sex BMI Region Charges
0 21 male 25.75 northeast 3279.87
1 37 female 25.74 southeast 21454.49
2 18 male 30.03 southeast 1720.35
3 37 male 30.68 northeast 6801.44
4 58 male 32.01 southeast 11946.63
5 46 male 26.62 southeast 7742.11
6 25 male 31.19 northeast 21736.33
Using tabulate for display
# Do the imports import pandas as pd from tabulate import tabulate # Read the csv file with selected columns df = pd.read_csv('test.csv', usecols=[0,1,2,4,6]) # Create new csv file df.to_csv('newcsv.csv', index=False) # Read new csv file new_df = pd.read_csv('newcsv.csv') # Using tabulate to format display output new_df = tabulate(new_df, headers=list(new_df), showindex=False, tablefmt='pretty') # Print new_df print(new_df)Output
Output:+-----+--------+-------+-----------+----------+
| Age | Sex | BMI | Region | Charges |
+-----+--------+-------+-----------+----------+
| 21 | male | 25.75 | northeast | 3279.87 |
| 37 | female | 25.74 | southeast | 21454.49 |
| 18 | male | 30.03 | southeast | 1720.35 |
| 37 | male | 30.68 | northeast | 6801.44 |
| 58 | male | 32.01 | southeast | 11946.63 |
| 46 | male | 26.62 | southeast | 7742.11 |
| 25 | male | 31.19 | northeast | 21736.33 |
+-----+--------+-------+-----------+----------+
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags