如何将带有“,”和“ / n”的字符串转换为pandas DataFrame?

时间:2019-02-20 09:58:28

标签: python pandas numpy dataframe

我有这段代码:

csvData = str(request.GET.get('csvData'))

print("TYPE csvData", type(csvData))

print("CSV", csvData)

arr = np.array(csvData.splitlines())

print("ARR", arr)

print("TYPE arr", type(arr))

csvData是一个字符串。

该代码显示以下输出:

  

TYPE csvData

     

CSV   “ NUM,AIRLINE_ARR_ICAO,WAKE,SIBT,SOBT,PLANNED_TURNAROUND,DISTANCE_FROM_ORIGIN,DISTANCE_TO_TARGET \ n1,AEA,H,2016年1月1日   04:05:00,2016-01-01 14:10:00,605,9920.67,5776.89 \ n2,AEA,H,2016-01-01   04:25:00,2016-01-01 06:30:00,125.0,10060.80,483.93 \ n3,AVA,H,2016-01-01   05:05:00,2016-01-01 07:05:00,120.0,8033.86,8033.86 \ n4,IBE,H,2016-01-01   05:20:00,2016-01-01 10:40:00,320.0,0.00,8507.73 \ n5,IBE,H,2016-01-01   05:25:00,2016-01-01 10:50:00,325.0,6698.42,6698.42 \ n6,IBE,H,2016-01-01   05:30:00,2016-01-01   08:10:00,160.0,10699.06,1246.30 \ n7,IBE,H,2016-01-01   05:30:00,2016-01-01 11:00:00,330.0,9081.35,8033.86 \ n8,IBE,H,2016-01-01   05:40:00,2016-01-01 11:35:00,355.0,5776.89,8749.87 \ n9,ANE,M,2016-01-01   05:50:00,2016-01-01 14:50:00,540.0,284.73,284.73 \ n10,ETD,H,2016-01-01   06:35:00,2016-01-01 08:00:00,85.0,5647.10,5647.10 \ n11,IBS,M,2016-01-01   06:50:00,2016-01-01 08:00:00,70.0,547.36,1460.92 \ n12,IBE,H,2016-01-01   06:50:00,2016-01-01   10:35:00,225.0,6763.16,6763.16 \ n13,IBE,H,2016-01-01   06:50:00,2016-01-01   10:50:00,240.0,7120.40,7120.40 \ n14,IBE,H,2016-01-01   06:50:00,2016-01-01 10:55:00,245.0,7010.08,0.00 \ n15,QTR,H,2016-01-01   06:55:00,2016-01-01 08:30:00,95.0,5338.52,5338.52 \ n16,IBS,M,2016-01-01   07:00:00,2016-01-01 07:45:00,45.0,485.52,1721.09 \ n17,IBS,M,2016-01-01   07:00:00,2016-01-01 07:45:00,45.0,394.98,429.37 \ n18,ELY,M,2016-01-01   07:05:00,2016-01-01 08:30:00,85.0,3550.48,3550.48 \ n19,AAL,H,2016-01-01   07:05:00,2016-01-01   12:05:00,300.0,5925.61,5925.61 \ n20,TVF,M,2016-01-01   07:30:00,2016-01-01 08:10:00,40.0,1030.31,1030.31 \ n“

     

ARR   ['“ NUM,AIRLINE_ARR_ICAO,WAKE,SIBT,SOBT,PLANNED_TURNAROUND,DISTANCE_FROM_ORIGIN,DISTANCE_TO_TARGET \ n1,AEA,H,2016年1月1日   04:05:00,2016-01-01 14:10:00,605,9920.67,5776.89 \ n2,AEA,H,2016-01-01   04:25:00,2016-01-01   06:30:00,125.0,10060.80,483.93 \ n3,AVA,H,2016-01-01   2016-05--01 05:05:00   07:05:00,120.0,8033.86,8033.86 \ n4,IBE,H,2016-01-01   05:20:00,2016-01-01 10:40:00,320.0,0.00,8507.73 \ n5,IBE,H,2016-01-01   05:25:00,2016-01-01   10:50:00,325.0,6698.42,6698.42 \ n6,IBE,H,2016-01-01   05:30:00,2016-01-01   08:10:00,160.0,10699.06,1246.30 \ n7,IBE,H,2016-01-01   05:30:00,2016-01-01   11:00:00,330.0,9081.35,8033.86 \ n8,IBE,H,2016-01-01   05:40:00,2016-01-01   11:35:00,355.0,5776.89,8749.87 \ n9,ANE,M,2016-01-01   05:50:00,2016-01-01 14:50:00,540.0,284.73,284.73 \ n10,ETD,H,2016-01-01   06:35:00,2016-01-01   08:00:00,85.0,5647.10,5647.10 \ n11,IBS,M,2016-01-01   06:50:00,2016-01-01 08:00:00,70.0,547.36,1460.92 \ n12,IBE,H,2016-01-01   06:50:00,2016-01-01   10:35:00,225.0,6763.16,6763.16 \ n13,IBE,H,2016-01-01   06:50:00,2016-01-01   10:50:00,240.0,7120.40,7120.40 \ n14,IBE,H,2016-01-01   06:50:00,2016-01-01 10:55:00,245.0,7010.08,0.00 \ n15,QTR,H,2016-01-01   06:55:00,2016-01-01   08:30:00,95.0,5338.52,5338.52 \ n16,IBS,M,2016-01-01   07:00:00,2016-01-01 07:45:00,45.0,485.52,1721.09 \ n17,IBS,M,2016-01-01   07:00:00,2016-01-01 07:45:00,45.0,394.98,429.37 \ n18,ELY,M,2016-01-01   07:05:00,2016-01-01   08:30:00,85.0,3550.48,3550.48 \ n19,AAL,H,2016-01-01   07:05:00,2016-01-01   12:05:00,300.0,5925.61,5925.61 \ n20,TVF,M,2016-01-01   07:30:00,2016-01-01 08:10:00,40.0,1030.31,1030.31 \ n“']

     

TYPE arr

我需要将arr转换为pandas DataFrame。我写了这段代码:

new = []
for i in range(0,len(arr)):
    line = arr[i].split(",")
    new.append(line)

X = pd.DataFrame(new[1:],columns=new[0])

print("X",X.head())

但是它不能正常工作。我认为它不起作用,因为arr['".."']而不是[..]

我们非常感谢您的帮助。

更新:

csvData = pd.read_csv(io.StringIO((request.GET.get('csvData'))))

print("TYPE csvData", type(csvData))

print("CSV", csvData.head())

TYPE csvData <class 'pandas.core.frame.DataFrame' CSV Empty DataFrame
Columns:
[NUM,AIRLINE_ARR_ICAO,WAKE,SIBT,SOBT,PLANNED_TURNAROUND,DISTANCE_FROM_ORIGIN,DISTANCE_TO_TARGET\n1,AEA,H,2016-01-01
04:05:00,2016-01-01 14:10:00,605,9920.67,5776.89\n2,AEA,H,2016-01-01
04:25:00,2016-01-01 06:30:00,125.0,10060.80,483.93\n3,AVA,H,2016-01-01
05:05:00,2016-01-01 07:05:00,120.0,8033.86,8033.86\n4,IBE,H,2016-01-01
05:20:00,2016-01-01 10:40:00,320.0,0.00,8507.73\n5,IBE,H,2016-01-01
05:25:00,2016-01-01 10:50:00,325.0,6698.42,6698.42\n6,IBE,H,2016-01-01
05:30:00,2016-01-01
08:10:00,160.0,10699.06,1246.30\n7,IBE,H,2016-01-01
05:30:00,2016-01-01 11:00:00,330.0,9081.35,8033.86\n8,IBE,H,2016-01-01
05:40:00,2016-01-01 11:35:00,355.0,5776.89,8749.87\n9,ANE,M,2016-01-01
05:50:00,2016-01-01 14:50:00,540.0,284.73,284.73\n10,ETD,H,2016-01-01
06:35:00,2016-01-01 08:00:00,85.0,5647.10,5647.10\n11,IBS,M,2016-01-01
06:50:00,2016-01-01 08:00:00,70.0,547.36,1460.92\n12,IBE,H,2016-01-01
06:50:00,2016-01-01
10:35:00,225.0,6763.16,6763.16\n13,IBE,H,2016-01-01
06:50:00,2016-01-01
10:50:00,240.0,7120.40,7120.40\n14,IBE,H,2016-01-01
06:50:00,2016-01-01 10:55:00,245.0,7010.08,0.00\n15,QTR,H,2016-01-01
06:55:00,2016-01-01 08:30:00,95.0,5338.52,5338.52\n16,IBS,M,2016-01-01
07:00:00,2016-01-01 07:45:00,45.0,485.52,1721.09\n17,IBS,M,2016-01-01
07:00:00,2016-01-01 07:45:00,45.0,394.98,429.37\n18,ELY,M,2016-01-01
07:05:00,2016-01-01 08:30:00,85.0,3550.48,3550.48\n19,AAL,H,2016-01-01
07:05:00,2016-01-01
12:05:00,300.0,5925.61,5925.61\n20,TVF,M,2016-01-01
07:30:00,2016-01-01 08:10:00,40.0,1030.31,1030.31\n] 

Index: []

更新2:

csvData = pd.read_csv(io.StringIO((request.GET.get('csvData').replace('\\n', '\n'))))

print("TYPE csvData", type(csvData))

print("CSV", csvData.head())

TYPE csvData <class 'pandas.core.frame.DataFrame'>
CSV Empty DataFrame

Columns: [NUM,AIRLINE_ARR_ICAO,WAKE,SIBT,SOBT,PLANNED_TURNAROUND,DISTANCE_FROM_ORIGIN,DISTANCE_TO_TARGET
1,AEA,H,2016-01-01 04:05:00,2016-01-01 14:10:00,605,9920.67,5776.89
2,AEA,H,2016-01-01 04:25:00,2016-01-01 06:30:00,125.0,10060.80,483.93
3,AVA,H,2016-01-01 05:05:00,2016-01-01 07:05:00,120.0,8033.86,8033.86
4,IBE,H,2016-01-01 05:20:00,2016-01-01 10:40:00,320.0,0.00,8507.73
5,IBE,H,2016-01-01 05:25:00,2016-01-01 10:50:00,325.0,6698.42,6698.42
6,IBE,H,2016-01-01 05:30:00,2016-01-01 08:10:00,160.0,10699.06,1246.30
7,IBE,H,2016-01-01 05:30:00,2016-01-01 11:00:00,330.0,9081.35,8033.86
8,IBE,H,2016-01-01 05:40:00,2016-01-01 11:35:00,355.0,5776.89,8749.87
9,ANE,M,2016-01-01 05:50:00,2016-01-01 14:50:00,540.0,284.73,284.73
10,ETD,H,2016-01-01 06:35:00,2016-01-01 08:00:00,85.0,5647.10,5647.10
11,IBS,M,2016-01-01 06:50:00,2016-01-01 08:00:00,70.0,547.36,1460.92
12,IBE,H,2016-01-01 06:50:00,2016-01-01 10:35:00,225.0,6763.16,6763.16
13,IBE,H,2016-01-01 06:50:00,2016-01-01 10:50:00,240.0,7120.40,7120.40
14,IBE,H,2016-01-01 06:50:00,2016-01-01 10:55:00,245.0,7010.08,0.00
15,QTR,H,2016-01-01 06:55:00,2016-01-01 08:30:00,95.0,5338.52,5338.52
16,IBS,M,2016-01-01 07:00:00,2016-01-01 07:45:00,45.0,485.52,1721.09
17,IBS,M,2016-01-01 07:00:00,2016-01-01 07:45:00,45.0,394.98,429.37
18,ELY,M,2016-01-01 07:05:00,2016-01-01 08:30:00,85.0,3550.48,3550.48
19,AAL,H,2016-01-01 07:05:00,2016-01-01 12:05:00,300.0,5925.61,5925.61
20,TVF,M,2016-01-01 07:30:00,2016-01-01 08:10:00,40.0,1030.31,1030.31
]

Index: []

更新3:

这是生成csvData的方式,

    var reader = new FileReader();
    reader.onload =  (e) => {
      // Use reader.result
      this.setState({
        csvData: reader.result
      })
      this.props.setCsvData(reader.result)
    }
    reader.readAsText(files[0])

然后我以这种方式将其发送到后端:

'&csvData='+JSON.stringify(this.state.csvData)

1 个答案:

答案 0 :(得分:2)

使用read_csv方法加载数据。

Specifications<PcPlacement> specification = Specifications.where(null);
Specifications<PcPlacement> specificationInner = Specifications.where(null);

specificationInner = specificationInner.or(buildReferringEntitySpecificationWithContains(
                            criteria.getUserFullName(), PcPlacement_.pcUser, PcUser_.fullName));

specificationInner = specificationInner.or(buildReferringEntitySpecificationWithContains(
                            criteria.getUserEmailId(), PcPlacement_.pcUser, PcUser_.emailId));

specification = specification.and(specificationInner);