我正在处理一个csv文件,并且一列由一个至多个整数值组成,这些值以逗号分隔。首先,代码将能够基于逗号知道行中的最大值。之后,它将生成名称为a / c的最大值的列。最后,它将能够在新列中打印值。我正在处理大数据。
INPUT
col column-name vvs
x12 2,3,4 ab
d3 4 fd
3fd 2,5,8,6 cf
5r4 3,2 mg
以下是我尝试编写的代码
a=read_file("xyz.csv") #READ FILE IS A FUNCTION AND IT WORKS PROPERLY
print(a["column-name"].max())
def dfmax(dfcl):
cl=0
t=[]
for i in range(dfcl.size):
b=dfcl[i]
c=b.split(",")
if len(c) > cl :
cl=len(c)
t=b
print(cl)
print(t)
return cl
c=dfmax(a["column-name"])
print(c)
for i in range(c):
a.insert(5+i,"A"+str(i+1),a["column-name"],True) #CODE IS WORKING FINE TILL HERE IT IS PRINTING MAXIMUM LENGTH
#I assume just to check the code that the maximum values in the column 5
c=5
#for i in range(a["column-name"].size):
for i in range(20):
b=a["column-name"][i]
cb=b.split(",")
for j in range(c):
if j < len(cb):
st = "A"+str(j+1)
#print(st)
a[st][i]=cb[j]
else:
a[st][i]=""
DESIRED OUTPUT
col column-name column-name-2 column-name-3 column-name-4 vvs
x12 2 3 4 ab
d3 4 fd
3fd 2 5 8 6 cf
5r4 3 2 mg
不幸的是,代码无法正常工作。它提供以下错误。如果有更简单的方法可以完成此任务,请告诉我。
C:\Users\abc\Anaconda3\lib\site-packages\ipykernel_launcher.py:10: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
# Remove the CWD from sys.path while we load stuff.
C:\Users\abc\Anaconda3\lib\site-packages\ipykernel_launcher.py:12: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
if sys.path[0] == '':