[英]Pivot Table in Pandas with two column(Index and Value)
I have a CSV file with obj
and VS
column.我有一个带有
obj
和VS
列的 CSV 文件。 I need to sum VS
values for each obj
and have output like below我需要对每个
obj
的VS
值求和,并具有如下所示的 output
Input:输入:
+-----+------+
| obj | VS |
+-----+------+
| B | 2048 |
| A | 1024 |
| B | 10 |
| A | 1024 |
| B | 1025 |
| A | 1026 |
| B | 1027 |
+-----+------+
Output: Output:
+---+------+
| A | 3074 |
+---+------+
| B | 4110 |
+---+------+
I have tried below code,As I have just two column to apply I added value
column with unique value to have pivot(pivot table need Index,Column and Value).Then Value
column is just to help.我已经尝试了下面的代码,因为我只有两列要应用,所以我添加了具有唯一值的
value
列来获得数据透视表(数据透视表需要索引、列和值)。然后Value
列只是为了提供帮助。 However out put is sum thing weird!!!但是输出总和很奇怪!
import pandas as pd
import numpy as np
filename='1test.csv'
df = pd.read_csv(filename, dtype='str')
df["value"]=1
pd.pivot_table(df, values="VS", index="obj", columns="value", aggfunc=np.sum)
output of my code :我的代码的 output :
+-------+----------------+
| value | 1 |
+-------+----------------+
| obj | |
| A | 102410241026 |
| B | 20481010251027 |
+-------+----------------+
Just consider that as you read from CSV, values are string, you need to convert them to int by df['VS']=pd.to_numeric(df['VS'])
print(df.dtypes)
show the type of column in df只需考虑,当您从 CSV 读取时,值是字符串,您需要通过
df['VS']=pd.to_numeric(df['VS'])
print(df.dtypes)
将它们转换为 int 显示列的类型在df中
import pandas as pd
import numpy as np
filename='1test.csv'
df = pd.read_csv(filename, dtype='str')
df["value"]=1
print(df.dtypes)
df['VS']=pd.to_numeric(df['VS'])
print(df.dtypes)
pd.pivot_table(df, values="VS", index="obj", columns="value", aggfunc=np.sum)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.