保存时,Pandas会添加一个空列

时间:2016-12-06 06:46:51

标签: python pandas

我有一个简单的程序,我想在列的末尾添加一个数字

fajl = pd.read_csv('example.csv')
print (fajl)
fajl.at[len(fajl['Hrana']),'Hrana'] = 350
print (fajl)
fajl.to_csv('example.csv')

首先打印是:

   Hrana  Izlasci
0   1641   6500.0
1    910      NaN
2   2100      NaN
3    371      NaN
    Hrana  Izlasci
0  1641.0   6500.0
1   910.0      NaN
2  2100.0      NaN
3   371.0      NaN
4   350.0      NaN

这是好事,这是我期望它做的,但当我第二次开始时:

   Unnamed: 0   Hrana  Izlasci
0           0  1641.0   6500.0
1           1   910.0      NaN
2           2  2100.0      NaN
3           3   371.0      NaN
4           4   350.0      NaN
   Unnamed: 0   Hrana  Izlasci
0         0.0  1641.0   6500.0
1         1.0   910.0      NaN
2         2.0  2100.0      NaN
3         3.0   371.0      NaN
4         4.0   350.0      NaN
5         NaN   350.0      NaN

它添加了一个新列,我不知道为什么。我是熊猫新手,所以有人可以解释为什么会这样,我该如何解决?

1 个答案:

答案 0 :(得分:1)

您需要将参数index=False添加到to_csv,以便不保存indexDataFrame中的第一列):

fajl.to_csv('example.csv', index=False)
fajl = pd.read_csv('example.csv')
print (fajl)
    Hrana  Izlasci
0   164.0   6500.0
1   910.0      NaN
2  2100.0      NaN
3   371.0      NaN
4   350.0      NaN

第二个读取csv的另一个解决方案是将index_col作为read_csv中的index添加参数fajl.to_csv('example.csv') fajl = pd.read_csv('example.csv', index_col=[0]) print (fajl) Hrana Izlasci 0 164.0 6500.0 1 910.0 NaN 2 2100.0 NaN 3 371.0 NaN 4 350.0 NaN

IEnumerator SpawnBalls () {

    Vector3[] ballArray = new Vector3[ballNumber];
    Vector3 spawnPosition = new Vector3 ();

    int index = 0;

    for (int i = 0; i < ballNumber; i++) {

        // Create first ball and store coordinates into our ballArray at index 0
        if (i == 0) {

            spawnPosition = Random.insideUnitCircle.normalized * 0.7f; 
            Instantiate (ball, spawnPosition, Quaternion.identity);
            Debug.Log ("wolo");
            ballArray[index] = spawnPosition;

        // Else we already have one ball
        } else if (i > 0) {
            // Create new spawn coordinates
            spawnPosition = Random.insideUnitCircle.normalized * 0.7f;

            // Loop through indicies of array that have been filled
            for( int k = 0; k <= index; k++ ) {
              // Check that each value in array meets distance requirement
              if(Vector3.Distance (spawnPosition, ballArray[k]) <= 0.3f) {
                // If not, create new coordinates and reset k
                spawnPosition = Random.insideUnitCircle.normalized * 0.7f;
                k = 0;
            }

            // Update array
            ballArray[index++] = spawnPosition;

            Instantiate (ball, spawnPosition, Quaternion.identity);
            Debug.Log ("yolo");
        }
        }
        yield return null;
    }