If you change the columns of dataframe, 0 will appear above the index.

Asked 1 years ago, Updated 1 years ago, 402 views

Load CSV data as described in the title in pd.csv_read and
If you change the columns in dataframe, you will see "0" above the index line.
If you run the data frame plot(), the legend will contain "0".
Please tell me how to delete this 0

CSV data is as follows:

import pandas aspd#pandas import

df = pd.read_csv('test_data.csv')
print(df)

Run Result A (test_data.csv)

Unnamed: 0 sine wave 010.25 Hz sine wave 020.2 Hz sine wave 030.3 Hz sine wave 040.15 Hz
0 Time [sec] Amplitude Amplitude Amplitude Amplitude
1            0            0             0             0            0
2          0.1  0.156434465   0.062666617    0.13116692  0.047054157
3          0.2  0.309016994   0.124344944   0.257687187  0.093690657
4          0.3    0.4539905   0.184062276   0.375078756  0.139495553
..         ...          ...           ...           ...          ...
97         9.6  0.587785252  -0.240876837  -0.479182974  0.184062276
98         9.7    0.4539905  -0.184062276  -0.375078756  0.139495553
99         9.8  0.309016994  -0.124344944  -0.257687187  0.093690657
100        9.9  0.156434465  -0.062666617   -0.13116692  0.047054157
10110 6.12574E-16-2.4503E-16-5.14562E-16 1.83772E-16

[102 rows x 5 columns]

The execution code is as follows

import pandas aspd#pandas import

# Read the first line of data.
temp=pd.read_csv("test_data.csv", header=None,nrows=1)
# Create Header on Line 1           
header = temp.iloc [0]

# Read only data after line 3
df=pd.read_csv("test_data.csv", header=None, skiprows=2)

# Specify header as column name
df.columns=header
print(df)

# Check Results Designate the first column as the horizontal axis
import japanize_matplotlib #Import Japanese Library
df.plot(x=df.columns[0])

Run Result B

0 NaN sine wave 010.25 Hz sine wave 020.2 Hz sine wave 030.3 Hz sine wave 040.15 Hz
00 0.000000e + 00 0.000000e + 00 0.000000e + 00 0.000000e + 00 0.000000e + 00
10.1 1.564345e-016.266662e-02 1.3116669e-014.705416e-02
20.2 3.090170e-011.243449e-012.576872e-019.369066e-02
30.34.539905e-011.840623e-013.750788e-011.394956e-01
40.45.877853e-012.408768e-014.791830e-011.840623e-01
..    ...           ...           ...           ...           ...
969.65.877853e-01-2.408768e-01-4.791830e-011.840623e-01
979.74.539905e-01-1.840623e-01-3.750788e-011.394956e-01
989.8 3.090170e-01-1.243449e-01-2.576872e-019.369066e-02
999.9 1.564345e-01-6.266662e-02-1.3116669e-014.705416e-02
100 10.0 6.125740e-16-2.450300e-16-5.145620e-16 1.837720e-16

[101 rows x 5 columns]
<AxesSubplot:xlabel='nan'>

Plot Results

The issues you want to resolve are as follows:

  • I would like to delete it because "0" appears in the upper left corner of dataframe in print(df) of execution result B.I want to know who this is again.
  • The legend of the plot in the execution result B shows "0" and I want to delete it.I want to know who this is again.

Tried
execution code,

#Read only data after line 3
df=pd.read_csv("test_data.csv", header=None, skiprows=2)

If you plotted df as of
"0" was not included.
I believe that "0" appears because of the following:

# Specify header in column name
df.columns=header

I apologize for the inconvenience, but I would appreciate it if you could let me know.
Thank you for your cooperation.

python pandas dataframe

2023-01-06 15:25

1 Answers

I understand that there are actually two header lines on the CSV side, and I want to ignore the second line, but when I write the action for that purpose, the name is also included in the unintended position.

When you do pd.read_csv, it is faster to ignore the first two lines in skiprows=2, but only the second line in skiprows=[1].

df=pd.read_csv('test_data.csv', skiprows=[1])

https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html

skiprows:list-like, intercallable, optional
Line numbers to skip(0-indexed) or number of lines to skip(int) at the start of the file.

The reason why 0 appears in some places in the question text is because 0 is set to name in df.columns in relation to illoc[0].


2023-01-06 19:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.