我一直想从pandas DataFrame中获取名称和索引位置,并陷入如下所示的逻辑中,只是想知道是否有更好的方法来处理此问题。
import pandas as pd
df = pd.read_csv('/docs/Credit_Card.csv', encoding='ISO-8859-1')
cols = df.columns
col = [df.columns.get_loc(c) for c in df.columns if c in cols]
print(pd.DataFrame(list(zip(cols, col)),columns=['index_Name', 'Index_Number']))
结果输出:
index_Name Index_Number
0 Card Type Code 0
1 Card Type Full Name 1
2 Issuing Bank 2
3 Card Number 3
4 Card Holder's Name 4
5 CVV/CVV2 5
6 Issue Date 6
7 Expiry Date 7
8 Billing Date 8
9 Card PIN 9
10 Credit Limit 10
答案 0 :(得分:2)
使用:
null
类似于numpy.c_
:
import numpy as np
print(pd.DataFrame(list(zip(df.columns,
np.arange(len(df.columns)))),
columns=['index_Name', 'Index_Number']))
或者:
print(pd.DataFrame(np.c_[df.columns, np.arange(len(df.columns))],
columns=['index_Name', 'Index_Number']))
答案 1 :(得分:1)
使用dict
{name: column_names: number: column_numering}
创建数据仓库。
In [591]: pd.DataFrame({'index_Name': df.columns, 'Index_Number': range(len(df.columns))})
Out[591]:
index_Name Index_Number
0 Card Type Code 0
1 Card Type Full Name 1
2 Issuing Bank 2
3 Card Number 3
4 Card Holder's Name 4
5 CVV/CVV2 5
6 Issue Date 6
7 Expiry Date 7
8 Billing Date 8
9 Card PIN 9
10 Credit Limit 10
或使用
In [621]: pd.DataFrame({'index_Name': df.columns}).rename_axis('index_Number').reset_index()
Out[621]:
index_Name Index_Number
0 Card Type Code 0
1 Card Type Full Name 1
2 Issuing Bank 2
3 Card Number 3
4 Card Holder's Name 4
5 CVV/CVV2 5
6 Issue Date 6
7 Expiry Date 7
8 Billing Date 8
9 Card PIN 9
10 Credit Limit 10