-2

I am editing a CSV file in Python, I have deleted some columns, created an index and filtered. But I have not been able to extract the numerical part of the columas. How can I extract only the numerical information from the columns?

Extract only the numeric part of the column data. example:

MarketTime = 11: 18: 26.549

The whole column should be like this:

11: 18: 26,549

import pandas as pd

df = pd.read_csv('C:/Users/TECNOLOGIA/datos.csv',names=['LocalTime', 'Message', 'MarketTime', 'Symbol', 'Type', 'Price', 'Size', 'Source','Condition','Tick','Mmid','SubMarketId','Date'], usecols=['Type','MarketTime','Price'],index_col='Type') df=(df.loc['Type=0']) print (df)

  • Possible duplicate of [How do I find numeric columns in Pandas?](https://stackoverflow.com/questions/25039626/how-do-i-find-numeric-columns-in-pandas) – aiven Jan 09 '18 at 22:19
  • 2
    Please include the data in the question, not as some image that might disappear in the future. – Ralf Stubner Jan 09 '18 at 22:21
  • 1
    we're not code writing service. Please also include what you tried and where you got stuck. Also, include the data array so that we can help you better – kmario23 Jan 09 '18 at 22:22
  • @Aiven: Nope, that isn't it. – cs95 Jan 09 '18 at 22:23
  • MarketTime Price Type Type=0 MarketTime=11:18:26.549 Price=112.8300 Type=0 MarketTime=11:18:28.792 Price=112.8300 Type=0 MarketTime=11:18:28.792 Price=112.8400 Type=0 MarketTime=11:18:28.792 Price=112.8300 Type=0 MarketTime=11:18:45.798 Price=112.8500 Type=0 MarketTime=11:18:45.799 Price=112.8500 Type=0 MarketTime=11:18:45.880 Price=112.8400 – Manuel Rincon Jan 09 '18 at 22:23
  • import pandas as pd df = pd.read_csv('C:/Users/TECNOLOGIA/datos.csv',names=['LocalTime', 'Message', 'MarketTime', 'Symbol', 'Type', 'Price', 'Size', 'Source','Condition','Tick','Mmid','SubMarketId','Date'], usecols=['Type','MarketTime','Price'],index_col='Type') df=(df.loc['Type=0']) print (df) – Manuel Rincon Jan 09 '18 at 22:24
  • This is the part of the code that I have written, but I can not eliminate the numerical part of the columns. I apologize, it's the first time I joined this forum. – Manuel Rincon Jan 09 '18 at 22:27
  • 1
    Please add your data as _text_ in your question. Pictures (or links of pictures) do not make it easy for us to answer the question. – cs95 Jan 09 '18 at 22:32

1 Answers1

0

Adapting the Regex/Pandas StringMethods answer given at pandas applying regex to replace values, you will have something like this:

import pandas as pd

df = pd.DataFrame(['MarketTime=11:18:28.792','MarketTime=11:18:28.792'], columns=['MarketTime'])

df['MarketTime'] = df['MarketTime'].str.extract(r'([\d:,.]+)')
print(df)
Alex Bausk
  • 690
  • 5
  • 29