使用pd.read_clipboard复制数据框时如何处理自定义命名索引? [英] How to handle custom named index when copying a dataframe using pd.read_clipboard?
问题描述
从其他问题中获得此数据框:
Given this data frame from some other question:
Constraint Name TotalSP Onpeak Offpeak
Constraint_ID
77127 aaaaaaaaaaaaaaaaaa -2174.5 -2027.21 -147.29
98333 bbbbbbbbbbbbbbbbbb -1180.62 -1180.62 0
1049 cccccccccccccccccc -1036.53 -886.77 -149.76
似乎有一个索引Constraint_ID
.当我尝试使用pd.read_clipboard
读取它时,这就是它的加载方式:
It seems like there is an index Constraint_ID
. When I try to read it in with pd.read_clipboard
, this is how it gets loaded:
Constraint Name TotalSP Onpeak Offpeak
0 Constraint_ID NaN NaN NaN NaN
1 77127 aaaaaaaaaaaaaaaaaa -2174.50 -2027.21 -147.29
2 98333 bbbbbbbbbbbbbbbbbb -1180.62 -1180.62 0.00
3 1049 cccccccccccccccccc -1036.53 -886.77 -149.76
这显然是错误的.我该如何纠正?
This is clearly wrong. How can I correct this?
推荐答案
read_clipboard
默认情况下使用空格分隔列.您看到的问题是由于第一列中的空白.如果您指定两个或多个空格作为分隔符,则根据表格式,它将找出索引列本身:
read_clipboard
by default uses whitespace to separate the columns. The problem you see is because of the whitespace in the first column. If you specify two or more spaces as the separator, based on the table format it will figure out the index column itself:
df = pd.read_clipboard(sep='\s{2,}')
df
Out:
Constraint Name TotalSP Onpeak Offpeak
Constraint_ID
77127 aaaaaaaaaaaaaaaaaa -2174.50 -2027.21 -147.29
98333 bbbbbbbbbbbbbbbbbb -1180.62 -1180.62 0.00
1049 cccccccccccccccccc -1036.53 -886.77 -149.76
index_col
参数还可以用于告诉熊猫第一列是索引,以防无法单独从分隔符推断出结构:
index_col
argument can also be used to tell pandas the first column is the index, in case the structure cannot be inferred from the separator alone:
df = pd.read_clipboard(index_col=0, sep='\s{2,}')
这篇关于使用pd.read_clipboard复制数据框时如何处理自定义命名索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!