从熊猫数据框中选择所有行的最简单方法是什么,谁的sym在整个表格中恰好出现两次?例如,在下表中,我想在[' b',' e']中选择所有带有sym的行,因为这些符号的value_counts等于2.
df=pd.DataFrame({'sym':['a', 'b', 'b', 'c', 'd','d','d','e','e'],'price':np.random.randn(9)})
price sym
0 -0.0129 a
1 -1.2940 b
2 1.8423 b
3 -0.7160 c
4 -2.3216 d
5 -0.0120 d
6 -0.5914 d
7 0.6280 e
8 0.5361 e
df.sym.value_counts()
Out[237]:
d 3
e 2
b 2
c 1
a 1
答案 0 :(得分:22)
我认为您可以sym
列使用groupby
,length == 2
使用filter
值:
print df.groupby("sym").filter(lambda x: len(x) == 2)
price sym
1 0.400157 b
2 0.978738 b
7 -0.151357 e
8 -0.103219 e
第二个解决方案使用isin
与boolean indexing:
s = df.sym.value_counts()
print s[s == 2].index
Index([u'e', u'b'], dtype='object')
print df[df.sym.isin(s[s == 2].index)]
price sym
1 0.400157 b
2 0.978738 b
7 -0.151357 e
8 -0.103219 e
使用transform
和boolean indexing
的最快解决方案:
print (df[df.groupby("sym")["sym"].transform('size') == 2])
price sym
1 -1.2940 b
2 1.8423 b
7 0.6280 e
8 0.5361 e
答案 1 :(得分:5)
您可以使用22:33:10,313 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [net.logstash.logback.appender.LogstashTcpSocketAppender]
22:33:10,379 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [LOGSTASH]
22:33:11,090 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.sampleapp] to INFO
22:33:11,093 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@565f390 - Propagating INFO level on Logger[com.sampleapp] onto the JUL framework
22:33:11,101 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [com.sampleapp] to false
22:33:11,102 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-ROLLING] to Logger[com.sampleapp]
22:33:11,105 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.hibernate] to INFO
22:33:11,105 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@565f390 - Propagating INFO level on Logger[org.hibernate] onto the JUL framework
22:33:11,106 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.hibernate] to false
22:33:11,106 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-ROLLING] to Logger[org.hibernate]
22:33:11,106 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.hibernate.SQL] to DEBUG
22:33:11,107 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@565f390 - Propagating DEBUG level on Logger[org.hibernate.SQL] onto the JUL framework
22:33:11,108 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.hibernate.SQL] to false
22:33:11,109 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-ROLLING] to Logger[org.hibernate.SQL]
22:33:11,111 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.hibernate.type.descriptor.sql] to DEBUG
22:33:11,112 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@565f390 - Propagating DEBUG level on Logger[org.hibernate.type.descriptor.sql] onto the JUL framework
22:33:11,112 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.hibernate.type.descriptor.sql] to false
22:33:11,113 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-ROLLING] to Logger[org.hibernate.type.descriptor.sql]
22:33:11,114 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.springframework] to INFO
22:33:11,114 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@565f390 - Propagating INFO level on Logger[org.springframework] onto the JUL framework
22:33:11,114 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.springframework] to false
22:33:11,114 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-ROLLING] to Logger[org.springframework]
22:33:11,115 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
22:33:11,116 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@565f390 - Propagating INFO level on Logger[ROOT] onto the JUL framework
22:33:11,116 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-ROLLING] to Logger[ROOT]
22:33:11,116 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [LOGSTASH] to Logger[ROOT]
22:33:11,116 |-INFO in ch.qos.logback.core.joran.action.ShutdownHookAction - About to instantiate shutdown hook of type [ch.qos.logback.core.hook.DelayingShutdownHook]
22:33:11,122 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
22:33:11,123 |-INFO in org.springframework.boot.logging.logback.SpringBootJoranConfigurator@448c8166 - Registering current configuration as safe fallback point
22:33:11,179 |-INFO in net.logstash.logback.appender.LogstashTcpSocketAppender[LOGSTASH] - Log destination localhost:4560: connection established.
22:33:11,192 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@565f390 - Propagating DEBUG level on Logger[com.sampleapp] onto the JUL framework
,它应该比使用echo -e "Arquivo nao-estruturado: \c"
read nome_arq
cp $nome_arq $nome_arq"_clean"
arq=$nome_arq"_clean"
sed -i 's/%//g;s/()//g;s/\.//g;s/\+//g;s/ Mai /_05_/g;s/ Abr /_04_/g;s/ Mar /\_03_/g;s/\,/\./g' $arq
sed -r -i 's/[[:space:]]+/,/g' $arq
sed -i 's/Data,Fechamento,Variação,Variação,Abertura,Máxima,Mínima,Volume/ref.date,price.close,var,var.perc,price.open,price.high,price.low,volume/g' $arq
和map
更快:
groupby
例如
transform
df[df['sym'].map(df['sym'].value_counts()) == 2]