我想删除启动特定" TYPE"的列。单词并且不包含_1
?
df =
TYPE_1 TYPE_2 TYPE_3 COL1
aaa asb bbb 123
结果应为:
df =
TYPE_1 COL1
aaa 123
目前我手动删除这些列,但是如果列数很大,这种方法效率不高:
df = df.drop(["TYPE_2","TYPE_3"], axis=1)
答案 0 :(得分:5)
可以使用列表理解。注意:axis = 1表示我们指的是列,inplace=True
也可以用作pandas.DataFrame.drop个文档。
droplist = [i for i in df.columns if i.startswith('TYPE') and '_1' not in i]
df1.drop(droplist,axis=1,inplace=True)
答案 1 :(得分:3)
这是第五个答案,但我想展示package raspberryws;
import java.io.IOException;
import java.io.InputStream;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("DHTService")
public class DHTService {
public DHTService() {
super();
}
@GET
@Produces(value = { "application/json" })
@Path("/readDHT")
public DHTSensor readDHT11() throws IOException {
DHTSensor sensor = new DHTSensor();
int ch;
char y;
String temp="" ;
String humidity="" ;
try {
Process p =
Runtime.getRuntime().exec("sudo python /projects/Adafruit_Python_DHT/examples/AdafruitDHT.py 11 4 ");
// get an InputStream for the python stdout
InputStream inStream = p.getInputStream();
// read all stdout data one char at a time.
while ((ch = inStream.read()) != -1) {
System.out.println(ch + " ch");
if ((int) ch == 47) {
sensor.setTempreture(temp);
humidity="";
} else {
if ((int) ch == 10) {
continue;
}
y = (char) ch;
temp=temp+y;
humidity=humidity+y;
sensor.setHumidity(humidity);
}
}
} catch (IOException e) {
e.printStackTrace();
}
// System.out.println(x);
return sensor;
}
数据框方法的强大功能,该方法按列名称和正则表达式进行过滤。这将搜索不以TYPE开头或在其中某处包含_1的列。
filter
答案 2 :(得分:2)
易:
unwanted = [column for column in df.columns
if column.startswith("TYPE") and "_1" not in column]
df = df.drop(unwanted)
答案 3 :(得分:1)
t_cols = [c for c in df.columns.values if c.startswith('TYPE_') and not c == 'TYPE_1']
df.drop(t_cols)
应该做的工作