Python Forum
Imported csv, column text converted to number but not summing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Imported csv, column text converted to number but not summing
#4
You should use pd.read_csv for this task. This function has many parameters which define the behavior of parsing data and they know the problem with comma or dot as decimal sign. Also the thousands sep can and should be specified. You should read this document: https://pandas.pydata.org/pandas-docs/st...d_csv.html

from textwrap import dedent
from io import StringIO


import pandas as pd


data = """
    source;rev
    a;1,00
    b;1.000,20
    c;123,45
    """

csv_data = StringIO(dedent(data.strip()))
# csv_data is a file-like object
# just for testing

df = pd.read_csv(csv_data, decimal=',', thousands='.', sep=';')
print(df['rev'])
Output:
0 1.00 1 1000.20 2 123.45 Name: rev, dtype: float64
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Imported csv, column text converted to number but not summing - by DeaD_EyE - Jun-21-2019, 08:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Parsing and summing time deltas (duration) onto bar + pie charts using pandas - - DRY Drone4four 2 669 Feb-10-2024, 06:04 PM
Last Post: Drone4four
  Make unique id in vectorized way based on text data column with similarity scoring ill8 0 924 Dec-12-2022, 03:22 AM
Last Post: ill8
  Pandas: summing columns conditional on the column labels ddd2332 0 2,172 Sep-10-2020, 05:58 PM
Last Post: ddd2332
  Combine a number into integer column and preserv format as number zinho 2 1,979 Dec-23-2019, 05:02 PM
Last Post: zinho
  Recognize text from number plate Sheheryar 4 3,883 Aug-27-2019, 04:00 AM
Last Post: Sheheryar
  Custom timeinterval converted to hourly values using Pandas? SinPy 1 2,841 Jun-07-2019, 05:06 AM
Last Post: heiner55
  Can't store pandas converted json dataframe into mongoDB mahmoud899 1 4,297 Dec-12-2018, 07:45 PM
Last Post: nilamo
  Text to column pandas ms5573 0 2,297 Aug-25-2018, 08:18 PM
Last Post: ms5573
  summing rows/columns more quickly jon0852 3 2,961 Feb-12-2018, 02:24 AM
Last Post: ka06059
  sklearn imported but not recognized kerberg 6 16,569 Jun-18-2017, 12:32 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020