想知道最简单的方法是将下面的数据帧索引拆分为子串,并将第二块设置为新数据帧中的列.
Input:
Ask Bid Last Open_Int Vol
245.0P 11.36 11.15 10.41 37 30
225.0C 10.31 10.23 10.3 52 5
224.5C 10.78 10.67 12 72 72
223.5C 11.68 11.56 12.68 89 59
244.5P 10.83 10.64 8.65 118 22
244.0P 10.34 10.15 9.93 137 10
Output:
Ask Bid Last Open_Int Vol Type
245.0P 11.36 11.15 10.41 37 30 P
225.0C 10.31 10.23 10.3 52 5 C
224.5C 10.78 10.67 12 72 72 C
223.5C 11.68 11.56 12.68 89 59 C
244.5P 10.83 10.64 8.65 118 22 P
244.0P 10.34 10.15 9.93 137 10 P
Run Code Online (Sandbox Code Playgroud)
df.assign(Type=df.index.str[-1])
Ask Bid Last Open_Int Vol Type
245.0P 11.36 11.15 10.41 37 30 P
225.0C 10.31 10.23 10.30 52 5 C
224.5C 10.78 10.67 12.00 72 72 C
223.5C 11.68 11.56 12.68 89 59 C
244.5P 10.83 10.64 8.65 118 22 P
244.0P 10.34 10.15 9.93 137 10 P
Run Code Online (Sandbox Code Playgroud)