I want display the data of the items listed in the show_list array. Obviously this code won't work. Any easy way to make it work?
import panda as pd
show_list=['Apple', 'Orange']
data = {
"Apple": [1,2,3],
"Banana": [4,5,6],
"Orange": [7,8,9]
}
df=pd.DataFrame(data)
for item in show_list:
print(df.item)
CodePudding user response:
If you just do df[show_list]
it'll display it.
In [1]: df[show_list]
Out[1]:
Apple Orange
0 1 7
1 2 8
2 3 9
CodePudding user response:
You can print the columns you want by passing their names to df
print(df[show_list])
The output will be
Apple Orange
0 1 7
1 2 8
2 3 9
If you have to do some operations with data in those columns you can do this
for (columnName, columnData) in df.items():
if columnName in show_list:
print('Column Name : ', columnName)
print('Column Contents : ', columnData.values)
for value in columnData.values:
# Do something with the value
Or
for item in show_list:
for index in range(0, len(df[item])):
print(f"{item}[{index}] = {df[item][index]}")