由于列和用户列表不同,因此会发出错误
" ValueError异常" Usecols与名称不匹配。
我怎样才能使用'如果列存在于csv?
中csv示例:
* {
box-sizing: border-box;
}
body {
background-color: #ffffff;
color: #666666;
background-image: url(../images/background.jpg);
font-family: Verdana, Arial, sans-serif;
}
#wrapper {
background-color: #90c7e3;
width: 80%;
min-width: 700px;
max-width: 1024px;
box-shadow: 10px 10px 5px #888888;
margin-right: auto;
margin-left: auto;
}
header {
background-color: #000033;
color: #ffffff;
font-family: Georgia, "Times New Roman", serif;
}
h1 {
background-image: url(../images/sunset.jpg);
background-repeat: no-repeat;
background-position: right;
padding-left: 20px;
height: 72px;
line-height: 200%;
margin-bottom: 0;
}
nav {
float: left;
width: 160px;
padding-top: 20px;
padding-right: 5px;
padding-left: 20px;
padding-bottom: 0;
padding: 5px;
font-weight: bold;
}
nav a {
text-decoration: none;
}
nav a:hover {
color: #ffffff;
}
nav a:link: {
color: #000033;
}
nav a:visited {
color: #344873;
}
nav ul {
list-style-type: none;
margin: 0;
padding-left: 0;
}
main {
background-color: #ffffff;
margin-left: 170px;
padding-top: 1px;
padding-bottom: 1px;
padding-left: 20px;
padding-right: 20px;
display: block;
}
h2 {
color: #3399cc;
font-family: Georgia, "Times New Roman", serif;
}
h3 {
color: #000033;
}
dt {
color: #000033;
font-weight: bold;
}
.resort {
color: #000033;
font-size: 1.2em;
}
#homehero {
background-image: url(../images/coast.jpg);
height: 300px;
background-size: 100% 100%;
margin-right: 0;
margin-left: 170px;
background-repeat: no-repeat;
}
#yurthero {
background-image: url(../images/yurt.jpg);
background-size: 100% 100%;
background-repeat: no-repeat;
height: 300px;
margin-left: 170px;
}
#trailhero {
background-image: url(../images/trail.jpg);
height: 300px;
background-size: 100% 100%;
background-repeat: no-repeat;
margin-left: 170px;
}
footer {
background-color: #ffffff;
margin-left: 170px;
font-size: .70em;
font-style: italic;
text-align: center;
padding: 10px;
}
header, nav, main, footer {
display: block;
}
阅读csv:
df.csv
AB,CD,EF,GH
foo,20160101,a,1
foo,20160102,a,3
foo,20160103,a,5
这就是我想要的:
import pandas as pd
df = pd.read_csv('df.csv',
header=0,usecols=["AB", "CD", "IJ"])
忽略" IJ"。
答案 0 :(得分:2)
使用lambda
中的usecols
跳过不在csv
中的列:
import pandas as pd
from io import StringIO
txt = """AB,CD,EF,GH
foo,20160101,a,1
foo,20160102,a,3
foo,20160103,a,5"""
usecols = ['AB', 'CD', 'IJ']
df = pd.read_csv(StringIO(txt), usecols=lambda c: c in set(usecols))
print(df)
AB CD
0 foo 20160101
1 foo 20160102
2 foo 20160103
答案 1 :(得分:1)
通常导入csv
import pandas as pd
from io import StringIO
txt = """AB,CD,EF,GH
foo,20160101,a,1
foo,20160102,a,3
foo,20160103,a,5"""
df = pd.read_csv(StringIO(txt))
print(df)
AB CD EF GH
0 foo 20160101 a 1
1 foo 20160102 a 3
2 foo 20160103 a 5
带有reindex
的 intersection
usecols = ['AB', 'CD', 'IJ']
df.reindex_axis(df.columns.intersection(usecols), 1)
AB CD
0 foo 20160101
1 foo 20160102
2 foo 20160103