Hướng dẫn python write dataframe to tab delimited file - python ghi khung dữ liệu vào tệp được phân định bằng tab

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận 

    1. Hãy cùng xem cách chuyển đổi một dữ liệu thành tệp CSV bằng cách sử dụng phân tách tab. Chúng tôi sẽ sử dụng phương thức TO_CSV () để lưu DataFrame dưới dạng tệp CSV. Để lưu DataFrame với các dấu phân cách tab, chúng ta phải vượt qua \ \ t, làm tham số SEP trong phương thức TO_CSV ().
    2. Cách tiếp cận: & nbsp;
    3. Nhập các mô -đun gấu trúc và numpy.
    4. Tạo DataFrame bằng phương thức DataFrame ().
    5. Lưu DataFrame dưới dạng tệp CSV bằng phương thức TO_CSV () với tham số SEP dưới dạng \ tiêu.

    Python3

    Tải tệp CSV mới được tạo bằng phương thức read_csv () dưới dạng dataFrame.

    Hiển thị DataFrame mới.

    import pandas as pd

    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    7
    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    8
    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    4
         Original DataFrame
       col1  col2  col3
    0     1     4     7
    1     4     5     8
    2     3     6     9
    3     4     7     0
    4     5     8     1
    Data from new_file.csv file:
      col1\tcol2\tcol3
    0          1\t4\t7
    1          4\t5\t8
    2          3\t6\t9
    3          4\t7\t0
    4          5\t8\t1             
    
    0
         Original DataFrame
       col1  col2  col3
    0     1     4     7
    1     4     5     8
    2     3     6     9
    3     4     7     0
    4     5     8     1
    Data from new_file.csv file:
      col1\tcol2\tcol3
    0          1\t4\t7
    1          4\t5\t8
    2          3\t6\t9
    3          4\t7\t0
    4          5\t8\t1             
    
    1

         Original DataFrame
       col1  col2  col3
    0     1     4     7
    1     4     5     8
    2     3     6     9
    3     4     7     0
    4     5     8     1
    Data from new_file.csv file:
      col1\tcol2\tcol3
    0          1\t4\t7
    1          4\t5\t8
    2          3\t6\t9
    3          4\t7\t0
    4          5\t8\t1             
    
    2
         Original DataFrame
       col1  col2  col3
    0     1     4     7
    1     4     5     8
    2     3     6     9
    3     4     7     0
    4     5     8     1
    Data from new_file.csv file:
      col1\tcol2\tcol3
    0          1\t4\t7
    1          4\t5\t8
    2          3\t6\t9
    3          4\t7\t0
    4          5\t8\t1             
    
    3
    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    2
         Original DataFrame
       col1  col2  col3
    0     1     4     7
    1     4     5     8
    2     3     6     9
    3     4     7     0
    4     5     8     1
    Data from new_file.csv file:
      col1\tcol2\tcol3
    0          1\t4\t7
    1          4\t5\t8
    2          3\t6\t9
    3          4\t7\t0
    4          5\t8\t1             
    
    5
    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    4
         Original DataFrame
       col1  col2  col3
    0     1     4     7
    1     4     5     8
    2     3     6     9
    3     4     7     0
    4     5     8     1
    Data from new_file.csv file:
      col1\tcol2\tcol3
    0          1\t4\t7
    1          4\t5\t8
    2          3\t6\t9
    3          4\t7\t0
    4          5\t8\t1             
    
    7
    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    4
         Original DataFrame
       col1  col2  col3
    0     1     4     7
    1     4     5     8
    2     3     6     9
    3     4     7     0
    4     5     8     1
    Data from new_file.csv file:
      col1\tcol2\tcol3
    0          1\t4\t7
    1          4\t5\t8
    2          3\t6\t9
    3          4\t7\t0
    4          5\t8\t1             
    
    9
    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    4
    >>> import collections
    >>> Q = collections.deque()
    >>> Q.append(1)
    >>> Q.appendleft(2)
    >>> Q.extend([3, 4])
    >>> Q.extendleft([5, 6])
    >>> Q
    deque([6, 5, 2, 1, 3, 4])
    >>> Q.pop()
    4
    >>> Q.popleft()
    6
    >>> Q
    deque([5, 2, 1, 3])
    >>> Q.rotate(3)
    >>> Q
    deque([2, 1, 3, 5])
    >>> Q.rotate(-3)
    >>> Q
    deque([5, 2, 1, 3])
    
    >>> last_three = collections.deque(maxlen=3)
    >>> for i in range(4):
    ...     last_three.append(i)
    ...     print ', '.join(str(x) for x in last_three)
    ...
    0
    0, 1
    0, 1, 2
    1, 2, 3
    2, 3, 4
    
    1
         Original DataFrame
       col1  col2  col3
    0     1     4     7
    1     4     5     8
    2     3     6     9
    3     4     7     0
    4     5     8     1
    Data from new_file.csv file:
      col1\tcol2\tcol3
    0          1\t4\t7
    1          4\t5\t8
    2          3\t6\t9
    3          4\t7\t0
    4          5\t8\t1             
    
    1

         Original DataFrame
       col1  col2  col3
    0     1     4     7
    1     4     5     8
    2     3     6     9
    3     4     7     0
    4     5     8     1
    Data from new_file.csv file:
      col1\tcol2\tcol3
    0          1\t4\t7
    1          4\t5\t8
    2          3\t6\t9
    3          4\t7\t0
    4          5\t8\t1             
    
    2
    >>> import collections
    >>> Q = collections.deque()
    >>> Q.append(1)
    >>> Q.appendleft(2)
    >>> Q.extend([3, 4])
    >>> Q.extendleft([5, 6])
    >>> Q
    deque([6, 5, 2, 1, 3, 4])
    >>> Q.pop()
    4
    >>> Q.popleft()
    6
    >>> Q
    deque([5, 2, 1, 3])
    >>> Q.rotate(3)
    >>> Q
    deque([2, 1, 3, 5])
    >>> Q.rotate(-3)
    >>> Q
    deque([5, 2, 1, 3])
    
    >>> last_three = collections.deque(maxlen=3)
    >>> for i in range(4):
    ...     last_three.append(i)
    ...     print ', '.join(str(x) for x in last_three)
    ...
    0
    0, 1
    0, 1, 2
    1, 2, 3
    2, 3, 4
    
    4
    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    2
    >>> import collections
    >>> Q = collections.deque()
    >>> Q.append(1)
    >>> Q.appendleft(2)
    >>> Q.extend([3, 4])
    >>> Q.extendleft([5, 6])
    >>> Q
    deque([6, 5, 2, 1, 3, 4])
    >>> Q.pop()
    4
    >>> Q.popleft()
    6
    >>> Q
    deque([5, 2, 1, 3])
    >>> Q.rotate(3)
    >>> Q
    deque([2, 1, 3, 5])
    >>> Q.rotate(-3)
    >>> Q
    deque([5, 2, 1, 3])
    
    >>> last_three = collections.deque(maxlen=3)
    >>> for i in range(4):
    ...     last_three.append(i)
    ...     print ', '.join(str(x) for x in last_three)
    ...
    0
    0, 1
    0, 1, 2
    1, 2, 3
    2, 3, 4
    
    6
    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    4
    >>> import collections
    >>> Q = collections.deque()
    >>> Q.append(1)
    >>> Q.appendleft(2)
    >>> Q.extend([3, 4])
    >>> Q.extendleft([5, 6])
    >>> Q
    deque([6, 5, 2, 1, 3, 4])
    >>> Q.pop()
    4
    >>> Q.popleft()
    6
    >>> Q
    deque([5, 2, 1, 3])
    >>> Q.rotate(3)
    >>> Q
    deque([2, 1, 3, 5])
    >>> Q.rotate(-3)
    >>> Q
    deque([5, 2, 1, 3])
    
    >>> last_three = collections.deque(maxlen=3)
    >>> for i in range(4):
    ...     last_three.append(i)
    ...     print ', '.join(str(x) for x in last_three)
    ...
    0
    0, 1
    0, 1, 2
    1, 2, 3
    2, 3, 4
    
    8
    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    4import0
    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    4import2import3

    import numpy as np

    students =

    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    0
    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    1
    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    2
    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    3
    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    4
    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    5
    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    
    6

    pandas as pd5

    >>> import collections
    >>> Q = collections.deque()
    >>> Q.append(1)
    >>> Q.appendleft(2)
    >>> Q.extend([3, 4])
    >>> Q.extendleft([5, 6])
    >>> Q
    deque([6, 5, 2, 1, 3, 4])
    >>> Q.pop()
    4
    >>> Q.popleft()
    6
    >>> Q
    deque([5, 2, 1, 3])
    >>> Q.rotate(3)
    >>> Q
    deque([2, 1, 3, 5])
    >>> Q.rotate(-3)
    >>> Q
    deque([5, 2, 1, 3])
    
    >>> last_three = collections.deque(maxlen=3)
    >>> for i in range(4):
    ...     last_three.append(i)
    ...     print ', '.join(str(x) for x in last_three)
    ...
    0
    0, 1
    0, 1, 2
    1, 2, 3
    2, 3, 4
    
    4pandas as pd7

    pandas as pd8pandas as pd9import0import1

    pandas as pd8import3

    import4import5import6=import8import1

    import4= import6

    pandas as pd8pandas as pd9numpy as np7import1

    pandas as pd8students 0

    Các 

    Hướng dẫn python write dataframe to tab delimited file - python ghi khung dữ liệu vào tệp được phân định bằng tab

    Hướng dẫn python write dataframe to tab delimited file - python ghi khung dữ liệu vào tệp được phân định bằng tab

    numpy as np0= numpy as np2import5import1

    Hướng dẫn python write dataframe to tab delimited file - python ghi khung dữ liệu vào tệp được phân định bằng tab

    Tôi phải định dạng lại dữ liệu của mình cho một phần mềm di truyền yêu cầu chia từng cột thành hai, ví dụ students 1. Tệp đầu ra được cho là phân loại tab. Tôi đang cố gắng làm điều đó trong gấu trúc:

    import csv
    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame(np.random.randint(0,3, size = (10,5)), 
                      columns=[ chr(c) for c in range(97, 97+5) ])
    
    def fake_alleles(x):
        if x==0:
            return "A\tA"
        if x==1:
            return "A\tG"
        if x==2:
            return "G\tG"
    
    plinkpast6 = df.applymap(fake_alleles)
    plinkpast6.to_csv("test.ped", sep="\t", quoting=csv.QUOTE_NONE)
    

    Điều đó cho tôi một lỗi students 2. Có những cách khác để làm điều đó với students 3 không?

    Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:51:41 (UTC/GMT +8 giờ)

    Gấu trúc: Tập thể dục DataFrame-27 với giải pháp

    Viết một chương trình gấu trúc để viết tệp dữ liệu vào tệp CSV bằng cách sử dụng phân tách tab.

    Dữ liệu mẫu: DataFrame gốc Col1 Col2 Col3 0 1 4 7 1 4 5 8 2 3 6 9 3 4 7 0 4 5 8 1 Dữ liệu từ New_File.CSV Tệp: COL1 \ TCOL2 \ TCOL3 0 1 \ T4 \ T7 1 4 \ T5 \ t8 2 3 \ t6 \ t9 3 4 \ t7 \ t0 4 5 \ t8 \ t1
    Original DataFrame
    col1 col2 col3
    0 1 4 7
    1 4 5 8
    2 3 6 9
    3 4 7 0
    4 5 8 1
    Data from new_file.csv file:
    col1\tcol2\tcol3
    0 1\t4\t7
    1 4\t5\t8
    2 3\t6\t9
    3 4\t7\t0
    4 5\t8\t1

    Giải pháp mẫu::

    Mã Python:

    import pandas as pd
    import numpy as np
    d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
    df = pd.DataFrame(data=d)
    print("Original DataFrame")
    print(df)
    print('Data from new_file.csv file:')
    df.to_csv('new_file.csv', sep='\t', index=False)
    new_df = pd.read_csv('new_file.csv')
    print(new_df)
    

    Đầu ra mẫu:

         Original DataFrame
       col1  col2  col3
    0     1     4     7
    1     4     5     8
    2     3     6     9
    3     4     7     0
    4     5     8     1
    Data from new_file.csv file:
      col1\tcol2\tcol3
    0          1\t4\t7
    1          4\t5\t8
    2          3\t6\t9
    3          4\t7\t0
    4          5\t8\t1             
    

    Trình chỉnh sửa mã Python-Pandas:

    Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn (và nhận xét) thông qua Disqus.

    Trước đây: Viết một chương trình gấu trúc để thêm một hàng vào DataFrame hiện có. Write a Pandas program to add one row in an existing DataFrame.
    Next: Write a Pandas program to count city wise number of people from a given of data set (city, name of the person).

    Python: Lời khuyên trong ngày

    Cấu trúc Deques (Deques là một khái quát của các ngăn xếp và hàng đợi):

    >>> import collections
    >>> Q = collections.deque()
    >>> Q.append(1)
    >>> Q.appendleft(2)
    >>> Q.extend([3, 4])
    >>> Q.extendleft([5, 6])
    >>> Q
    deque([6, 5, 2, 1, 3, 4])
    >>> Q.pop()
    4
    >>> Q.popleft()
    6
    >>> Q
    deque([5, 2, 1, 3])
    >>> Q.rotate(3)
    >>> Q
    deque([2, 1, 3, 5])
    >>> Q.rotate(-3)
    >>> Q
    deque([5, 2, 1, 3])
    
    >>> last_three = collections.deque(maxlen=3)
    >>> for i in range(4):
    ...     last_three.append(i)
    ...     print ', '.join(str(x) for x in last_three)
    ...
    0
    0, 1
    0, 1, 2
    1, 2, 3
    2, 3, 4
    

    Làm cách nào để lưu gấu trúc dưới dạng tệp được phân định tab?

    Cách tiếp cận :..
    Nhập các mô -đun Gandas và Numpy ..
    Tạo DataFrame bằng phương thức DataFrame () ..
    Lưu DataFrame dưới dạng tệp CSV bằng phương thức TO_CSV () với tham số SEP dưới dạng \ \ t Hồi ..
    Tải tệp CSV mới được tạo bằng phương thức read_csv () dưới dạng dataFrame ..
    Hiển thị DataFrame mới ..

    Làm thế nào để bạn viết một khung dữ liệu trong một tệp văn bản trong Python?

    Làm cách nào để chuyển đổi DataFrame thành tệp văn bản?Sử dụng np.savetxt () để viết nội dung của dataFrame vào một tệp văn bản.Use np. savetxt() to write the contents of a DataFrame into a text file.

    Làm thế nào để bạn viết một khung dữ liệu vào tệp CSV trong Python?

    Bằng cách sử dụng phương thức pandas.dataframe.to_csv (), bạn có thể ghi/lưu/xuất/xuất một tệp dữ liệu pandas sang tệp CSV.Theo mặc định, TO_CSV () Xuất DataFrame vào tệp CSV với dấu phân cách dấu phẩy và chỉ mục hàng làm cột đầu tiên. DataFrame. to_csv() method you can write/save/export a pandas DataFrame to CSV File. By default to_csv() method export DataFrame to a CSV file with comma delimiter and row index as the first column.

    Làm cách nào để lưu DataFrame vào tệp CSV?

    Xuất dữ liệu dữ liệu vào tệp CSV pandas dataFrame to_csv () Chức năng Xuất định dạng DataFrame sang định dạng CSV.Nếu một đối số tệp được cung cấp, đầu ra sẽ là tệp CSV.Mặt khác, giá trị trả về là định dạng CSV như chuỗi.SEP: Chỉ định một dấu phân cách tùy chỉnh cho đầu ra CSV, mặc định là dấu phẩy.Pandas DataFrame to_csv() function exports the DataFrame to CSV format. If a file argument is provided, the output will be the CSV file. Otherwise, the return value is a CSV format like string. sep: Specify a custom delimiter for the CSV output, the default is a comma.