Load & Import First Column from CSV File as pandas DataFrame in Python (Example Code)
In this Python tutorial you’ll learn how to read only the first column of a CSV file as a new pandas DataFrame.
Setting up the Example
import pandas as pd # Import pandas library |
import pandas as pd # Import pandas library
my_df = pd.DataFrame({'A':range(100, 105), # Construct new pandas DataFrame 'B':[9, 3, 7, 1, 2], 'C':['a', 'f', 'c', 'f', 'f'], 'D':range(35, 30, - 1)}) print(my_df) # A B C D # 0 100 9 a 35 # 1 101 3 f 34 # 2 102 7 c 33 # 3 103 1 f 32 # 4 104 2 f 31 |
my_df = pd.DataFrame({'A':range(100, 105), # Construct new pandas DataFrame 'B':[9, 3, 7, 1, 2], 'C':['a', 'f', 'c', 'f', 'f'], 'D':range(35, 30, - 1)}) print(my_df) # A B C D # 0 100 9 a 35 # 1 101 3 f 34 # 2 102 7 c 33 # 3 103 1 f 32 # 4 104 2 f 31
my_df.to_csv('my_df.csv') # Write exemplifying DataFrame to folder |
my_df.to_csv('my_df.csv') # Write exemplifying DataFrame to folder
Example: Read First pandas DataFrame from CSV File
my_df_load = pd.read_csv('my_df.csv', usecols = ['A']) # Import CSV & print output print(my_df_load) # A # 0 100 # 1 101 # 2 102 # 3 103 # 4 104 |
my_df_load = pd.read_csv('my_df.csv', usecols = ['A']) # Import CSV & print output print(my_df_load) # A # 0 100 # 1 101 # 2 102 # 3 103 # 4 104
Further Resources & Related Articles
Below, you may find some further resources on topics such as data conversion, extracting data, and lists.