Reputation: 8826
I have CSV file So, when parsing this file with pandas with ISO-8859-1
encoding. However i'm just trying to create a DataFrame df_cols
to print only selected columns but it giving the error on execution as it has metachars like /
'
(example 'Card Holder's Name', 'CVV/CVV2'
) hence fails to get the output.
#!/grid/common/pkgs/python/v3.6.1/bin/python3
##### Pandas Display Setting for the complete output on the terminal ####
import pandas as pd
pd.set_option('display.height', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('expand_frame_repr', True)
df_list = pd.read_csv('/docs/Credit_Card.csv', encoding='ISO-8859-1')
df_cols = df_list[['Card Type Full Name', 'Issuing Bank', 'Card Number', 'Card Holder's Name', 'CVV/CVV2', 'Issue Date', 'Expiry Date','Credit Limit']]
print(df_cols)
Upvotes: 0
Views: 63
Reputation: 8826
As suggested by d_kennetz , we can directly read the columns based on names or index position on the DataFrame df_list
itself which will reduce the time and resource utilization( memory consumption) to read the whole CSV.
As mentioned there are two way to read the columns first based on names where we need to be extra careful about special/metachars whereas the second method based on the index position we don't need to care about this which is slightly more useful to avoid this glitch.
df_list = pd.read_csv('/docs/Credit_Card.csv', encoding='ISO-8859-1',usecols=['Card Type Full Name', 'Issuing Bank', 'Card Number', 'Card Holder\'s Name', 'CVV/CVV2', 'Issue Date', 'Expiry Date','Credit Limit'])
OR
df_list = pd.read_csv('/docs/Credit_Card.csv', encoding='ISO-8859-1',usecols=[1, 2, 3, 4, 5, 6, 7, 10])
Upvotes: 0
Reputation: 18647
Try escaping the single quote character with \
df_cols = df_list[['Card Type Full Name', 'Issuing Bank', 'Card Number', 'Card Holder\'s Name', 'CVV/CVV2', 'Issue Date', 'Expiry Date','Credit Limit']]
Upvotes: 1
Reputation: 741
try put column name in three quotation mark
"""Card Holder's Name"""
Upvotes: 1