Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在数据帧中转换_Python_Pandas - Fatal编程技术网

Python 在数据帧中转换

Python 在数据帧中转换,python,pandas,Python,Pandas,我有一张这样的清单- body = ['"name","number"', '"Dudh & Pani Dudh & Pani Dudh Wala","+91 70148 05126"', '"Deepanshu Paymate","+91 72082 94015"'] df = pd.DataFrame(body) print(df) 输出为- 0 0

我有一张这样的清单-

body = ['"name","number"', '"Dudh & Pani Dudh & Pani Dudh Wala","+91 70148 05126"', '"Deepanshu Paymate","+91 72082 94015"']

df = pd.DataFrame(body)
print(df)
输出为-

                                                   0
0                                    "name","number"
1  "Dudh & Pani Dudh & Pani Dudh Wala","+91 70148...
2              "Deepanshu Paymate","+91 72082 94015"
0                               "name"           "number"
1  "Dudh & Pani Dudh & Pani Dudh Wala"  "+91 70148 05126"
2                  "Deepanshu Paymate"  "+91 72082 94015"

但我需要两列,第一列将命名,第二列将为数字。

使用列表理解和拆分,所有数据不包含列表的第一个值,对于列,拆分列表的第一个值:

如果想要脱衣舞:

替换的另一个想法:

我是这样发现的-

body = ['"name","number"', '"Dudh & Pani Dudh & Pani Dudh Wala","+91 70148 05126"', '"Deepanshu Paymate","+91 72082 94015"']

data = pd.DataFrame(body)

# new data frame with split value columns 
data = data[0].str.split(",", n = 1, expand = True) 

new_header = data.iloc[0] #grab the first row for the header
data = data[1:] #take the data less the header row
data.columns = new_header #set the header row as the df header

print(data)
输出为-

                                                   0
0                                    "name","number"
1  "Dudh & Pani Dudh & Pani Dudh Wala","+91 70148...
2              "Deepanshu Paymate","+91 72082 94015"
0                               "name"           "number"
1  "Dudh & Pani Dudh & Pani Dudh Wala"  "+91 70148 05126"
2                  "Deepanshu Paymate"  "+91 72082 94015"

从您的字符串列表中,我们通过理解列表创建一个 成对的列表[名称,编号],并从这个新列表中创建您的 数据帧:

 In[16]: new_body = [element.split(",")  for element in body]
 In[17]: df = pd.DataFrame(new_body)

 In[18]: df
 Out[18]:
                                      0                  1
 0                               "name"           "number"
 1  "Dudh & Pani Dudh & Pani Dudh Wala"  "+91 70148 05126"
 2                  "Deepanshu Paymate"  "+91 72082 94015"
而不是

body = ['"name","number"', '"Dudh & Pani Dudh & Pani Dudh Wala","+91 70148 05126"', '"Deepanshu Paymate","+91 72082 94015"']
使用

一,。E与其用撇号“…”包围线对,不如用方括号“[…]”包围线对

然后,使用df=pd.DataFramebody创建的数据帧将

body = ['"name","number"', '"Dudh & Pani Dudh & Pani Dudh Wala","+91 70148 05126"', '"Deepanshu Paymate","+91 72082 94015"']
body = [["name","number"], ["Dudh & Pani Dudh & Pani Dudh Wala","+91 70148 05126"], ["Deepanshu Paymate","+91 72082 94015"]]
                                   0                1
0                               name           number
1  Dudh & Pani Dudh & Pani Dudh Wala  +91 70148 05126
2                  Deepanshu Paymate  +91 72082 94015