Create New pandas DataFrame from Existing Data in Python (2 Examples) | Extract Specific Columns

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

import pandas as pd # Load pandas

data = pd.DataFrame({'x1':range(7, 12), # Create example DataFrame
'x2':['a', 'b', 'c', 'd', 'e'],
'x3':range(6, 1, - 1),
'x4':[5, 9, 2, 8, 7]})
print(data) # Print example DataFrame

print(data_new1) # Print copy

data_new2 = data[['x2', 'x4']].copy() # Extract selected columns
print(data_new2) # Print DataFrame subset

Follow me on Social Media:

Рекомендации по теме
Комментарии
Автор

how to use a list of column names instead of explicitly mentioning them like you have mentioned x2 & x4?

ligesh