我有一个41年的数据集,我想用熊猫模块做一些统计计算。但是,我对熊猫的知识缺乏。
下面是一个csv文件数据集示例:

date    day month   year    pcp1    pcp2    pcp3    pcp4    pcp5    pcp6
1.01.1979   1   1   1979    0.431   2.167   9.375   0.431   2.167   9.375
2.01.1979   2   1   1979    1.216   2.583   9.162   1.216   2.583   9.162
3.01.1979   3   1   1979    4.041   9.373   23.169  4.041   9.373   23.169
4.01.1979   4   1   1979    1.799   3.866   8.286   1.799   3.866   8.286
5.01.1979   5   1   1979    0.003   0.051   0.342   0.003   0.051   0.342
6.01.1979   6   1   1979    2.345   3.777   7.483   2.345   3.777   7.483
7.01.1979   7   1   1979    0.017   0.031   0.173   0.017   0.031   0.173
8.01.1979   8   1   1979    5.061   5.189   43.313  5.061   5.189   43.313

这是我的代码:
import numpy as np
import pandas as pd
import csv

filename="output813b.csv"
cols = ["date","year","month","day" ,"pcp1","pcp2","pcp3","pcp4","pcp5","pcp6"]
data1=pd.read_csv(filename,sep=',', header=None,names=cols,usecols=range(1,9))
colmns_needed=["month" ,"pcp1","pcp2","pcp3","pcp4","pcp5","pcp6"]
data2=pd.read_csv(filename,sep=',', header=None,names=colmns_needed)
mm=data2.groupby("month")
print(mm.sum())
print('\n')

但PCP列下的值似乎存储为字符串。
以下是pcp1的输出示例:
Month  pcp1

1      0.4310.4720000.91800000.01011.63904.65900.5780...
10     00.1500000000.027000.02400.1630.9610000000.017...
11     00.4940000000000.0480.003012.26200000003.612.9...
12     0.1890.0760.47000000000.08800.1080.26107.15000...
13     00.06500.1060.00700000050.6207.1510.0860.1487....
14     0000.64200000000.017025.5910.93400.04500000000...
15     0.742000.0720000000000.32500000000002.9877.512...
16     6.43900000000000.38103.986000000000033.5534.76...
17     0.0890000.2750000.555001.9230.562.9130.1360000...
18     3.28200000000.024000.656002.1750000000008.2434...
19     1.28200000000000000.0070000000007.0383.0450.17...
2      1.2160.1050000000010.4690.2092.9700.0415.6062....
20     00.4960.05100000000000.3550.1582.8530.04600000...
21     00000000000002.69903.5190.13000002.830.5151.09...
22     0000000007.19600000000000001.4421.76500.04500....
23     0000000008.168000.02100000000000.1083.8760.968...

我怎么能解决这个问题?

最佳答案

不要在header=None调用中指定read_csv。您告诉函数数据中没有标题行,根据上面发布的示例数据,文件的第一行是标题。因此,它将第一个头行视为数据,因此混合值如pcp10.431,并将所有列解释为字符串。

关于python - Pandas 分组方式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39426428/

10-09 19:07