Select Columns of pandas DataFrame by Index in Python (2 Examples) | Extract One Or Multiple Columns

preview_player
Показать описание
Python code of this video:

import pandas as pd # Import pandas library to Python

data = pd.DataFrame({'x1':range(100, 105), # Create pandas DataFrame
'x2':['a', 'd', 'd', 'a', 'b'],
'x3':range(36, 31, - 1),
'x4':['a', 'b', 'c', 'd', 'e'],
'x5':range(27, 22, - 1),
'x6':['x', 'z', 'y', 'z', 'x']})
print(data) # Print pandas DataFrame

print(data_new1) # Print DataFrame with one variable

col_select = [1, 3, 5] # Specify indices of columns to select
print(col_select) # Print list of indices
# [1, 3, 5]

print(data_new2) # Print DataFrame with multiple variables

Follow me on Social Media:

Рекомендации по теме